Using routes
- Home
- Developing modules
- Using routes
Routes provide a mapping between incoming urls and controllers. They are the only way to access the front-end controllers; there aren't any implied urls.
They're defined in config file named routes.php
as an array. The key is a regular expression and the value is the route destination.
Route destinations are the namespaced controller name, the method name, and optionally arguments. The segments are separated by slashes. It's important to remember the difference between slashes and backslashes. A destination is in the form:
<namespace-controller>/<method>/<arg-one>/<arg-two>/<arg-n>
Regex replacements for arguments (e.g. $1
) can be used.
So to route to the blog post view (with the post id accessable in the first regex variable) the destination would be something like:
SproutModules\AcmeInc\Controllers\BlogController/viewPost/$1
The regexes for route selection are implied to be anchored to the start and end of the string. The first matching regex will be used; subsequent routes will be ignored.
modules/blog/config/routes.php
// Calls BlogController::list $config['blog/list'] = 'SproutModules\AcmeInc\Controllers\BlogController/list'; // Calls BlogController::view with two arguments, the post id and the output type $config['blog/view/([0-9]+)/json'] = 'SproutModules\AcmeInc\Controllers\BlogController/view/$1/json'; $config['blog/view/([0-9]+)'] = 'SproutModules\AcmeInc\Controllers\BlogController/view/$1/html';