Download the PHP package andrewcarteruk/simple-route without Composer
On this page you can find all versions of the php package andrewcarteruk/simple-route. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package simple-route
SimpleRoute
This easy to use router is a simple wrapper for the FastRoute library.
Install
Install using Composer.
Example Usage
Documentation
The following documentation is derived from the FastRoute documentation.
Routes are defined as an array of SimpleRoute\Route
objects that are passed to
SimpleRoute\Router::fromArray(array $routes)
.
SimpleRoute\Route
objects require a $method
, a $pattern
and a $handler
:
The $method
is an uppercase HTTP method string for which a certain route
should match. It is possible to specify multiple valid methods using an array:
By default the $pattern
uses a syntax where {foo}
specifies a placeholder
with name foo
and matching the regex [^/]+
. To adjust the pattern the
placeholder matches, you can specify a custom pattern by writing {bar:[0-9]+}
.
Some examples:
Custom patterns for route placeholders cannot use capturing groups. For example
{lang:(en|de)}
is not a valid placeholder, because ()
is a capturing group.
Instead you can use either {lang:en|de}
or {lang:(?:en|de)}
.
Furthermore parts of the route enclosed in [...]
are considered optional, so
that /foo[bar]
will match both /foo
and /foobar
. Optional parts are only
supported in a trailing position, not in the middle of a route.
The $handler
parameter does not necessarily have to be a callback, it could
also be a controller class name or any other kind of data you wish to associate
with the route. SimpleRoute only tells you which handler corresponds to your
URI, how you interpret it is up to you.
Credits
This library is merely a wrapper for FastRoute that aims to provide an easier to use API.
The author of FastRoute is Nikita Popov.