Download the PHP package onesimus-systems/osrouter without Composer
On this page you can find all versions of the php package onesimus-systems/osrouter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download onesimus-systems/osrouter
More information about onesimus-systems/osrouter
Files in onesimus-systems/osrouter
Package osrouter
Short Description A simple, easy to use router for PHP web applications
License BSD-3-Clause
Informations about the package osrouter
OSRouter
OSRouter is a simple, fast HTTP router for PHP. It can perform simple routes with variable and optional URL parts. OSRouter can extract parts of a URL as variable that can be passed to a closure or a controller method.
Requirements
- PHP >= 5.4.0
Usage
The main methods are Router::get()
, ::post()
, and ::any()
. The signature for each is ($pattern, $callback, $options = [])
. \$pattern is the URI pattern the route will match using the format /static/{variable}/{?option-variable}
. \$callback is either a string such as Controller@method
or a closure function. \$options can be a single string in which case it will interpreted as a single filter, or it can be an array with the syntax ['filter' => ['filter1', 'filter2']]
. The outer array is to allow for extra options that may be added later on.
To route in an application located in a subdirectory of the webserver, you'll need to remove the directory prefix from the REQUEST_URI field in the Request object. For example, if the base of your application was located at http://example.com/blog
, you will need to strip "/blog" from the request uri before processing a route. Otherwise, all routes will be checked against "/blog/something" instead of just "/something".
You can also register routes in a group
Groups are defined using the Router::group(array $options, array $routes)
method. \$options is a keyed array with the keys 'filter', 'prefix', and 'rprefix'. 'Filter' is an array of filters that apply to the group. A single filter can be given as a string as well. 'Prefix' is a prefix added to the HTTP pattern in each group. In the example above, the routes will be /admin/groups/{?id}
and /admin/users/{?id}
. 'Rprefix' is prepended to each controller statement. For example, if 'rprefix' => '\Namespace\Admin\'
was added to the group above, the controller statements would be \Namespace\Admin\Controller@groups
and \Namespace\Admin\Controller@user
.
Note: Closures cannot be assigned to a route defined in a group. To assign a closure, assign the route separately.
You can define a 404 route using the method Router::register404Route($callback, $options = [])
. If no 404 route is defined and a route isn't found, a RouteException
will be thrown.
Filters
Filters can be defined and assigned to routes. Routes can have multiple filters, they will be handled in the order they're defined on the route. If all filters return a non-falsey value, the route will be green-lit and the dispatch will continue. Otherwise a FailedFilterException
will be thrown. Also, a RouteException
will be thrown if the filter isn't defined.
Example:
When the above route is dispatched, the 'auth' filter will be executed and if it returns a non-falsey value, the AdminController will be given control as normal. You can define multiple filters using an array.
In this case both filters must return truthy.