on 18 November 2013 by in
Lately, we’ve been going over the migration of v2 packages from v1. Today, I’ll talk about the updated Aura.Router v2 package. While not an example of extracting a new package from an existing one, it has a couple of features that other routers don’t currently have, in addition to being truly independent and completely decoupled from any other package.
(This post describes only some of the features of the router; please visit the Aura.Router v2 package page for full information. Also, unlike v1, the v2 package is PHP 5.3 compatible!)
Anyone using a framework system built in the past 5 years or so should already be familiar with routers. A router takes the incoming URL path, along with other optional data, and extracts a series of values that determine what controller and action should be executed, along with the parameters to send along to the controller and action.
Most routing systems combine the “routing” task with the “dispatch” task. That is, they both extract the parameters and pick the controller/action. Aura.Router v2, because of its truly independent nature, only does routing. It turns out that dispatching is something that can be independent of routing, and so we have a separate Aura.Dispatcher package to handle that (although you can use any dispatching system you like).
Aura.Router v2 has the basic features one expects: map a path to a route, define token regexes, etc.
Because Aura.Router v2 is completely decoupled, you have to feed it the incoming URL path and server information when doing matching (it does not make assumptions about the execution environment):
If there is a match, the $route
variable will be a Route object (otherwise
false). You can then dispatch to the controller and action of your choice
by getting the $route->params
; the following is naive but works just fine
when autoloading is set up:
If you’re looking for an independent dispatching system with a little more power and flexibility, checkout Aura.Dispatcher.
Aura.Router v2 allows you to examine the $_SERVER
values and pick a
route based on them using the addServer()
method. For example, if you want
to match routes based on the HTTP_ACCEPT
value …
In that naive example, there must be a $_SERVER['HTTP_ACCEPT']
string that
matches the given regex for the route to match. The matching HTTP_ACCEPT
portion will be placed in the $route->params
for you to use as you wish.
Sometimes we need a params in the route path to be sequentially optional.
The classic example is a blog archive organized by year, month, and day.
We don’t want to have to write three routes, one for /{year}
, /{year}/{month}
,
and /{year}/{month}/{day}
, each with repeated information about the route.
In Aura.Router v2, there is a special notation similar to URI Templates that indicates sequentially optional params:
With that, the following routes will all match the ‘archive’ route, and will set the appropriate values:
/blog/archive
/blog/archive/1979
/blog/archive/1979/11
/blog/archive/1979/11/07
Incidentally, this means that the commonly used controller/action/id catchall route is easy to implement with defaults in place:
That means /
will have params indicating the default
controller index
action, /foo
will indicate the foo
controller index
action, /foo/bar
will indicate the foo
controller bar
action, and /foo/bar/42
will
indicate the foo
controller bar
action with id
of 42.
Aura.Router v2 is different from v1 in how it attaches what are variously called sub-routes or route groups. Previously, we used a descriptor array, but in practice that turned out to be a little unwieldly. The new version uses a callable to allow you attach route groups programmatically:
See a longer example here.
As a side note, the router will automatically add default controller
and
action
values for you here. The controller
value is the route name prefix,
and the action
value is the sub-route name. Of course, these can be
overridden by path params or your own default values.
As a consequence of using a closure to add route groups, we can now add REST
resource routes in a single call to attachResource()
:
This will add seven REST routes with appropriate names, paths, HTTP methods, and token regexes; you can see the list here.
If you decide those routes are not to your liking, you can override the
default behavior by using setResourceCallable()
to pass callable of your own
to create resource routes:
That example will cause four CRUD routes to be added when you call
attachResource()
.
The Aura.Router v2 library retains the strengths of the v1 offering while adding convenience and power, all in a completely decoupled package with no other dependencies. It keeps a separation between routing and dispatching, allowing you to drop it into any system you like and use any dispatching mechanism of your own choice.
If you like clean code, fully decoupled libraries, and truly independent packages, then the Aura project is for you. Download a single package and start using it in your project today, with no added dependencies.
Subscribe and get latest updates by email
blog comments powered by Disqus