mirror of
https://github.com/dannyvankooten/AltoRouter.git
synced 2025-01-17 12:18:16 +01:00
Changed the way routes are named (last parameter of map method is no longer an array). Also AltoRouter is now throwing an Exception when you're trying to map two routes with the same name.
This commit is contained in:
parent
14be0eaad0
commit
c22b42912a
@ -11,13 +11,19 @@ class AltoRouter {
|
||||
/**
|
||||
* Map a route to a target
|
||||
*/
|
||||
public function map($method, $route, $target, array $args = array()) {
|
||||
public function map($method, $route, $target, $name = null) {
|
||||
|
||||
$route = $this->basePath . $route;
|
||||
$this->routes[] = array($method, $route, $target);
|
||||
|
||||
$this->routes[] = array($method, $route, $target, $name);
|
||||
|
||||
if(isset($args['name'])) {
|
||||
$this->namedRoutes[$args['name']] = $route;
|
||||
if($name) {
|
||||
if(isset($this->namedRoutes[$name])) {
|
||||
throw new \Exception("Can not redeclare route '{$name}'");
|
||||
} else {
|
||||
$this->namedRoutes[$name] = $route;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -58,6 +64,9 @@ class AltoRouter {
|
||||
*/
|
||||
public function match($requestUrl = null, $requestMethod = null) {
|
||||
|
||||
$params = array();
|
||||
$match = false;
|
||||
|
||||
// set Request Url if it isn't passed as parameter
|
||||
if($requestUrl === null) {
|
||||
$requestUrl = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';
|
||||
@ -78,7 +87,7 @@ class AltoRouter {
|
||||
$_REQUEST = array_merge($_GET, $_POST);
|
||||
|
||||
foreach($this->routes as $handler) {
|
||||
list($method, $_route, $target) = $handler;
|
||||
list($method, $_route, $target, $name) = $handler;
|
||||
|
||||
$methods = explode('|', $method);
|
||||
$method_match = false;
|
||||
@ -131,15 +140,18 @@ class AltoRouter {
|
||||
|
||||
|
||||
|
||||
if(isset($match) && $match > 0) {
|
||||
if(($match == true || $match > 0)) {
|
||||
|
||||
if($params) {
|
||||
foreach($params as $key => $value) {
|
||||
if(is_numeric($key)) unset($params[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
return array(
|
||||
'target' => $target,
|
||||
'params' => $params
|
||||
'params' => $params,
|
||||
'name' => $name
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -4,10 +4,10 @@ require 'AltoRouter.php';
|
||||
|
||||
$router = new AltoRouter();
|
||||
$router->setBasePath('/AltoRouter');
|
||||
$router->map('GET|POST','/', 'home#index', array('name' => 'home'));
|
||||
$router->map('GET|POST','/', 'home#index', 'home');
|
||||
$router->map('GET','/users/', array('c' => 'UserController', 'a' => 'ListAction'));
|
||||
$router->map('GET','/users/[i:id]', 'users#show', array('name' => 'users_show'));
|
||||
$router->map('POST','/users/[i:id]/[delete|update:action]', 'usersController#doAction', array('name' => 'users_do'));
|
||||
$router->map('GET','/users/[i:id]', 'users#show', 'users_show');
|
||||
$router->map('POST','/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do');
|
||||
|
||||
// match current request
|
||||
$match = $router->match();
|
||||
@ -18,6 +18,7 @@ $match = $router->match();
|
||||
<pre>
|
||||
Target: <?php var_dump($match['target']); ?>
|
||||
Params: <?php var_dump($match['params']); ?>
|
||||
Name: <?php var_dump($match['name']); ?>
|
||||
</pre>
|
||||
|
||||
<h3>Try these requests: </h3>
|
||||
|
Loading…
x
Reference in New Issue
Block a user