5.3. Basic Routes

The heart of the RewriteRouter is the definition of user defined routes. Routes are created by calling the addRoute method of RewriteRouter:

$router->addRoute('user', 'user/:username');

The first parameter is the name of the route. It is redundant at the moment of writing but will be used in the future in a URL view helper to allow for easy URL generation in your views. Should you need to make use of a previously configured named route, you can retrieve it with the getRoute method of the RewriteRouter.

The second parameter is a route that will be matched to a URL - for example, the above route will match http://example.com/user/martel. The colon in a route marks a URL variable which will be accessible through a Zend_Controller_Action::_getParam method. In our example a parameter named username will be set to a value of 'martel'.

[Note] Note
Routes are matched in reverse order so make sure your most generic routes are defined first.
[Note] Note
For now the current implementation allows for use of any characters except a slash (/) as a variable identifier but it is strongly recommended that one uses only php variable friendly characters. In future the implementation will probably be altered and this may introduce bugs to your code.

There are two special variables which can be used in your routes - 'controller' and 'action'. These special variables will be used to find a controller and/or an action chosen in the URL. The 'action' variable must always be defined either in the route or as a default parameter. The 'controller' variable will default to the IndexController if it is not defined.

$router->addRoute('user', ':controller/:action');

If you point your browser to 'http://example.com/news/latest' with this route defined the Zend_Controller_Dispatcher will invoke the latestAction of your NewsController.