Download the PHP package maer/router without Composer
On this page you can find all versions of the php package maer/router. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package router
Short Description A small, simple, extendable one-file PHP router with groups, filters and named routes
License MIT
Informations about the package router
A small and simple PHP router
A small, simple, extendable one-file PHP router with groups, filters and named routes
I'm not claiming that this router is faster or better than other routers out there. It's kind of hard to beat something like nikic/FastRoute. The two main reasons for building this was: 1. I wanted a simple but yet flexible, plug and play router with minimal to none setup. 2. It's fun to build stuff and you learn a lot from it!
Usage
- Install
- Simple Example
- Route parameters
- Route callbacks
- Filters
- Named routes
-
- Prepend base url
- Grouping routes
- Group prefix
- Crud routes
- Redirects
- Redirect current request to a named route
- Dispatch the router
- Not found
- Method not allowed
- Adding a custom callback resolver
- Release notes
Install
Clone this repository or use composer to download the library with the following command:
Change dev-master
to the last tagged release.
Simple example
Route parameters
There are some placeholders you can use for route parameters. All parameters will be passed along to the controller in the same order as they are defined in the route:
Route callbacks
Route callbacks can be defined in different ways:
All callbacks will receive any route parameter.
If you send in a class method (non static), the router will instantiate the class and then call the method, when the router is dispatched and a match is found.
Filters
There are before
and after
filters:
Filter callbacks can be in the same formats as Route callbacks, meaning that you can use class methods as filters.
The before filter will receive all route parameter, just like the route callback. The after filter will also receive all parameters, but the first parameter will be be the response from the route callback.
Note: Filters will be called in the same order as they were defined. If any filter returns anything other than null
, the dispatch will stop and that response will be returned instead.
Named routes
Add a name to any route
If you don't pass enough arguments to cover all required parameters, an exception will be thrown.
Prepend base url
By default, the router returns paths without the base url (protocol + hostname)
However, if you want the response from getRoute()
to include the base url, you need to set the base url:
When you now want to get a named route, you can pass a boolean as third argument. true
= include base url and false
won't.
If you don't want to pass a third argument, but rather have it returning the base url as default, you can set that using:
If you call getRoute()
without the third argument, it will always prepend the base url unless you pass false
as third argument to getRoute()
.
Grouping routes
Instead of adding the same filters over and over for many routes, it's easier to group them together.
The $r->group()
-method only takes an anonymous function as callback. The router instance is always passed as an argument to the callback.
When defining a group, you can add before
and after
filters, just like you do for routes. You can also use a prefix
for a group, as described below.
Group Prefix
To add the same prefix to a group, use the prefix
argument.
You can mix before
, after
and prefix
when creating groups.
CRUD routes
To simplify the creation of CRUD routes, there's a crud()
-function.
The above is the same as if you would define the following routes:
You can of course use the crud()
function inside a group as well.
Redirects
You can register the router to redirect certain URL's as well.
Note: All redirect routes are triggered before any other registered routes.
Register a redirect:
You can also register before
-filters on a redirect.
Redirect current request to a named route
When you register i redirect using the above method, it will be handled on dispatch, just like any other route.
Sometimes you want to redirect the user straight away, maybe after the router already has been dispatched (or even before). You can use toRoute()
for that:
The above will redirect the request immediately. It's just like doing header('location: ...')
.
Dispatch the router
To dispatch the router, it's usually enough to just call the $r->dispatch()
-method. How ever, if you want to dispatch the router with some specific URL and method you can pass them to the dispatcher (this is useful if you're writing tests):
If you rather trigger all the callbacks (filters and route callbacks) yourself, if you, for example, are using an IoC container, call the $r->getMatch()
method instead and you will get the matched route object back.
If the before and after filters are closures, you can trigger them via:
Not found
If there is no match, a Maer\Router\NotFoundException
will be thrown. You can register a callback that will be executed instead, using the $router->notFound()
-method:
Method not allowed
If there is a url match but with the wrong http verb, a Maer\Router\MethodNotAllowedException
will be thrown. You can register a callback that will be executed instead, using the $router->methodNotAllowed()
-method:
Adding a custom callback resolver
If your callback is in the format of ['Classname', 'method']
, you might want to customize how it's resolved. This is handy if you, for example, are using some kind of IoC with dependency injection.
To create your custom resolver, use the $r->resolver()
-method. Example:
Release notes
1.5.0
- Added redirect-method redirect()
- Added redirect-method toRoute()
1.4.0
- Added crud()-method.
If you have any questions, suggestions or issues, let me know!
Happy coding!