PHP5.3+ Routing Class. Supports REST, dynamic and reversed routing.
View the Project on GitHub dannyvankooten/AltoRouter
Download ZIP View on GitHubBy now, you should have rewritten al requests to be handled by a single file in which you created an AltoRouter instance.
To map your routes, use the map()
method. The following example maps all GET /
requests.
The map()
method accepts the following parameters.
$method
(string)
This is a pipe-delimited string of the accepted HTTP requests methods.
Example: GET|POST|PATCH|PUT|DELETE
$route
(string)
This is the route pattern to match against. This can be a plain string, one of the predefined regex filters or a custom regex. Custom regexes must start with @
.
Examples:
Route | Example Match | Variables |
---|---|---|
/contact/ |
/contact/ |
|
/users/[i:id]/ |
/users/12/ |
$id: 12 |
/[a:c]/[a:a]?/[i:id]? |
/controller/action/21 |
$c: "controller", $a: "action", $id: 21 |
$target
(mixed)
As AltoRouter leaves handling routes up to you, this can be anything.
Example using a function callback:
function() { ... }
Example using a controller#action string:
UserController#showDetails
$name
(string, optional)
If you want to use reversed routing, specify a name parameter so you can later generate URL's using this route.
Example:
user_details
For quickly adding multiple routes, you can use the addRoutes
method. This method accepts an array or any kind of traversable.
$router->addRoutes(array(
array('PATCH','/users/[i:id]', 'users#update', 'update_user'),
array('DELETE','/users/[i:id]', 'users#delete', 'delete_user')
));
You can use the following limits on your named parameters. AltoRouter will create the correct regexes for you.
The character before the colon (the 'match type') is a shortcut for one of the following regular expressions
You can register your own match types using the addMatchTypes()
method.
Once your routes are all mapped you can start matching requests and continue processing the request.