The routing for the framework is made possible with the help of Aura.Router.
Aura Router is a PHP package that implements web routing. Given a URI path and
a copy of $_SERVER
, it will extract controller, action, and parameter values
for a specific application route.
Your application foundation or framework is expected to take the information
provided by the matching route and dispatch to a controller on its own. As
long as your system can provide a URI path string and a representative copy of
$_SERVER
, you can use Aura Router.
Aura Router is inspired by Solar rewrite rules and http://routes.groovie.org.
To create a route, call the add()
method.
To generate a URI path from a route so that you can create links, call
generate()
on the router object and provide the route name.
Aura Router does not do dynamic matching of routes; a route must have a name to be able to generate a path from it.
The example shows that passing an array of data as the second parameter will
cause that data to be interpolated into the route path. This data array is
optional. If there are path params without matching data keys, those params
will not be replaced, leaving the {:param}
token in the path. If there are
data keys without matching params, those values will not be added to the path.
When you add a complex route specification, you describe extra information related to the path as an array with one or more of the following recognized keys:
params
– The regular expression subpatterns for path params; inline
params will override these settings. For example:Note that the path itself is allowed to contain param tokens with inline
regular expressions; e.g., /read/{:id:(\d+)}
. This may be easier to read in some cases.
values
– The default values for the route. These will be overwritten
by matching params from the path.method
– The $server['REQUEST_METHOD']
must match one of these values.
secure
– When true
the $server['HTTPS']
value must be on, or the
request must be on port 443; when false
, neither of those must be in place.
routable
– When false
the route will not be used for matching,
only for generating paths.
is_match
– A custom callback or closure with the signature
function(array $server, \ArrayObject $matches)
that returns true on a
match, or false if not. This allows developers to build any kind of matching
logic for the route, and to change the $matches
for param values from the path.
generate
– A custom callback or closure with the signature
function(\aura\router\Route $route, array $data)
that returns a modified
$data
array to be used when generating the path.
Here is a full route specification named read
with all keys in place:
Note that using closures, instead of callbacks, means you will not be able to
serialize()
or var_export()
the router for caching.
You don’t need to specify a complex route specification. If you pass a string for the route instead of an array …
… then Aura Router will use a default subpattern that matches everything
except slashes for the path params, and use the route name as the default
value for 'action'
. Thus, the above short-form route is equivalent to the
following long-form route:
Sometimes it is useful to allow the trailing part of the path be anything at all. There are two types of such “wildcard” routes. (Wildcard routing of this sort works only when specified at the end of the path.)
The first is a “values optional” named wildcard, represented by adding
/{:foo*}
to the end of the path. This will allow the route to match
anything after that point, including nothing at all. On a match, it will
collect the remaining slash-separated values into a sequential array named
'foo'
. Notably, the matched path with no wildcard values may have a slash at
the end or not.
The second is a “values required” wildcard, represented by adding /{:foo+}
to the end of the path. This will allow the route to match anything at all
after that point, but there must be at least one slash and an additional
value. On a match, it will collect the remaining slash-separated values into a
sequential array named 'foo'
.
N.b.: In previous releases of the router,
'/*'
was the wildcard indicator, with wildcard values collected in an array named'*'
. This behavior remains available but is deprecated.
You can add a series of routes all at once under a single “mount point” in
your application. For example, if you want all your blog-related routes to be
mounted at '/blog'
in your application, you can do this:
Each of the route paths will be prefixed with /blog
, so the effective paths
become:
browse: /blog/
read: /blog/{:id}{:format}
edit: /blog/{:id}/edit
You can set other route specification keys as part of the attachment specification; these will be used as the defaults for each attached route, so you don’t need to repeat common information:
RouterMap objects are available inside controller as $this->router
You can generate routes as