Routing Component¶
Overview¶
The Phalcon\Mvc\Router component allows you to define routes that are mapped to controllers or handlers that receive and can handle the request. The router has two modes: MVC mode and match-only mode. The first mode is ideal for working with MVC applications.
<?php
use Phalcon\Mvc\Router;
$router = new Router();
$router->add(
'/admin/invoices/list',
[
'controller' => 'invoices',
'action' => 'list',
]
);
$router->handle(
$_SERVER["REQUEST_URI"]
);
Constants¶
There are two constants available for the Phalcon\Mvc\Router component that are used to define the position of the route in the processing stack.
POSITION_FIRST
POSITION_LAST
Methods¶
Phalcon\Mvc\Router constructor
public function add(
string $pattern,
mixed $paths = null,
mixed $httpMethods = null,
int $position = Router::POSITION_LAST
): RouteInterface
use Phalcon\Mvc\Router;
$router->add("/about", "About::index");
$router->add(
"/about",
"About::index",
["GET", "POST"]
);
$router->add(
"/about",
"About::index",
["GET", "POST"],
Router::POSITION_FIRST
);
public function addConnect(
string $pattern,
mixed $paths = null,
int $position = Router::POSITION_LAST
): RouteInterface
CONNECT
public function addDelete(
string $pattern,
mixed $paths = null,
int $position = Router::POSITION_LAST
): RouteInterface
DELETE
public function addGet(
string $pattern,
mixed $paths = null,
int $position = Router::POSITION_LAST
): RouteInterface
GET
public function addHead(
string $pattern,
mixed $paths = null,
int $position = Router::POSITION_LAST
): RouteInterface
HEAD
public function addOptions(
string $pattern,
mixed $paths = null,
int $position = Router::POSITION_LAST
): RouteInterface
OPTIONS
public function addPatch(
string $pattern,
mixed $paths = null,
int $position = Router::POSITION_LAST
): RouteInterface
PATCH
public function addPost(
string $pattern,
mixed $paths = null,
int $position = Router::POSITION_LAST
): RouteInterface
POST
public function addPurge(
string $pattern,
mixed $paths = null,
int $position = Router::POSITION_LAST
): RouteInterface
PURGE
(Squid and Varnish support) public function addPut(
string $pattern,
mixed $paths = null,
int $position = Router::POSITION_LAST
): RouteInterface
PUT
public function addTrace(
string $pattern,
mixed $paths = null,
int $position = Router::POSITION_LAST
): RouteInterface
TRACE
public function attach(
RouteInterface $route,
int $position = Router::POSITION_LAST
): RouterInterface
use Phalcon\Mvc\Router;
use Phalcon\Mvc\Router\Route;
class CustomRoute extends Route {
// ...
}
$router = new Router();
$router->attach(
new CustomRoute(
"/about",
"About::index",
["GET", "HEAD"]
),
Router::POSITION_FIRST
);
Defining Routes¶
Phalcon\Mvc\Router provides advanced routing capabilities. In MVC mode, you can define routes and map them to controllers/actions that you require. A route is defined as follows:
<?php
use Phalcon\Mvc\Router;
$router = new Router();
$router->add(
'/admin/invoices/list',
[
'controller' => 'invoices',
'action' => 'list',
]
);
$router->add(
'/admin/customers/list',
[
'controller' => 'customers',
'action' => 'list',
]
);
$router->handle(
$_SERVER["REQUEST_URI"]
);
The first parameter of the add()
method is the pattern you want to match and, optionally, the second parameter is a set of paths. In the above example, for the URI /admin/invoices/list
, the InvoicesController
will be loaded and the listAction
will be called. It is important to remember that the router does not execute the controller and action, it only collects this information and then forwards it to the Phalcon\Mvc\Dispatcher which executes them.
An application can have many paths and defining routes one by one can be a cumbersome task. Phalcon\Mvc\Router offers an easier way to register routes.
<?php
use Phalcon\Mvc\Router;
$router = new Router();
$router->add(
'/admin/:controller/:action/:params',
[
'controller' => 1,
'action' => 2,
'params' => 3,
]
);
In the example above, we are using wildcards to make a route valid for many URIs. For example, accessing the following URL (/admin/customers/view/12345/1
) would produce:
Controller | Action | Parameter | Parameter |
---|---|---|---|
customers | view | 12345 | 1 |
The add()
method receives a pattern that can optionally have predefined placeholders and regular expression modifiers. All the routing patterns must start with a forward slash character (/
). The regular expression syntax used is the same as the PCRE regular expressions.
NOTE
It is not necessary to add regular expression delimiters. All route patterns are case-insensitive.
The second parameter defines how the matched parts should bind to the controller/action/parameters. Matching parts are placeholders or subpatterns delimited by parentheses (round brackets). In the example given above, the first subpattern matched (:controller
) is the controller part of the route, the second the action (:action
), and after that any parameters passed (:params
).
These placeholders make the route expressions more readable and easier to understand. The following placeholders are supported:
Placeholder | Regular Expression | Matches |
---|---|---|
/:module | /([a-zA-Z0-9\_\-]+) | Valid module name with alpha-numeric characters only |
/:controller | /([a-zA-Z0-9\_\-]+) | Valid controller name with alpha-numeric characters only |
/:action | /([a-zA-Z0-9_-]+) | Valid action name with alpha-numeric characters only |
/:params | (/.*)* | List of optional words separated by slashes. Only use this placeholder at the end of a route |
/:namespace | /([a-zA-Z0-9\_\-]+) | Single level namespace name |
/:int | /([0-9]+) | Integer parameter |
Controller names are camelized, this means that characters (-
) and (_
) are removed and the next character is uppercased. For instance, some_controller
is converted to SomeController
.
Since you can add as many routes as needed using the add()
method, the order in which routes are added indicates their relevance. The routes added last have more relevance than the ones added above them. Internally, all defined routes are traversed in reverse order until Phalcon\Mvc\Router finds the one that matches the given URI and processes it, while ignoring the rest.
Named Parameters¶
The example below demonstrates how to define names to route parameters:
<?php
$router->add(
// 1 / 2 / 3 / 4
'/admin/([0-9]{4})/([0-9]{2})/([0-9]{2})/:params',
[
'controller' => 'invoices',
'action' => 'view',
'year' => 1, // ([0-9]{4})
'month' => 2, // ([0-9]{2})
'day' => 3, // ([0-9]{2})
'params' => 4, // :params
]
);
In the above example, the route does not define a controller
or action
. Those are replaced with fixed values (invoices
and view
). The user will never know the underlying controller that is dispatched by the request. In the controller, those named parameters can be accessed as follows:
<?php
use Phalcon\Mvc\Controller;
use Phalcon\Mvc\Dispatcher;
/**
* @property Dispatcher $dispatcher
*/
class InvoicesController extends Controller
{
public function viewAction()
{
// year
$year = $this->dispatcher->getParam('year');
// month
$month = $this->dispatcher->getParam('month');
// day
$day = $this->dispatcher->getParam('day');
// ...
}
}
Note that the values of the parameters are obtained from the dispatcher. There is also another way to create named parameters as part of the pattern:
<?php
$router->add(
'/admin/{year}/{month}/{day}/{invoiceNo:[0-9]+}',
[
'controller' => 'invoices',
'action' => 'view',
]
);
You can access their values in the same way as before:
<?php
use Phalcon\Mvc\Controller;
use Phalcon\Mvc\Dispatcher;
/**
* @property Dispatcher $dispatcher
*/
class InvoicesController extends Controller
{
public function viewAction()
{
// year
$year = $this->dispatcher->getParam('year');
// month
$month = $this->dispatcher->getParam('month');
// day
$day = $this->dispatcher->getParam('day');
// invoiceNo
$invoiceNo = $this->dispatcher->getParam('invoiceNo');
// ...
}
}
Short Syntax¶
Phalcon\Mvc\Router also offers an alternative, shorter syntax. The following examples produce the same result:
<?php
$router->add(
'/admin/{year:[0-9]{4}}/{month:[0-9]{2}}/{day:[0-9]{2}}/:params',
'Invoices::view'
);
$router->add(
'/admin/([0-9]{4})/([0-9]{2})/([0-9]{2})/:params',
[
'controller' => 'invoices',
'action' => 'view',
'year' => 1, // ([0-9]{4})
'month' => 2, // ([0-9]{2})
'day' => 3, // ([0-9]{2})
'params' => 4, // :params
]
);
Array and Short Syntax¶
Array and short syntax can be mixed to define a route, in this case, note that named parameters automatically are added to the route paths according to the position on which they were defined:
<?php
$router->add(
'/admin/{year:[0-9]{4}}/([0-9]{2})/([0-9]{2})/:params',
[
'controller' => 'invoices',
'action' => 'view',
'month' => 2, // ([0-9]{2}) // 2
'day' => 3, // ([0-9]{2}) // 3
'params' => 4, // :params // 4
]
);
year
. Modules¶
You can define routes with modules in the path. This is especially suitable for multimodule applications. You can define a default route that includes a module wildcard.
<?php
use Phalcon\Mvc\Router;
$router = new Router(false);
$router->add(
'/:module/:controller/:action/:params',
[
'module' => 1,
'controller' => 2,
'action' => 3,
'params' => 4,
]
);
With the above route, you need to always have the module name as part of your URL. For example, for the following URL: /admin/invoices/view/12345
, will be processed as:
Module | Controller | Action | Parameter |
---|---|---|---|
admin | invoices | view | 12345 |
Or you can bind specific routes to specific modules:
<?php
$router->add(
'/login',
[
'module' => 'session',
'controller' => 'login',
'action' => 'index',
]
);
$router->add(
'/invoices/:action',
[
'module' => 'admin',
'controller' => 'invoices',
'action' => 1,
]
);
Or bind them to specific namespaces:
<?php
$router->add(
'/:namespace/login',
[
'namespace' => 1,
'controller' => 'login',
'action' => 'index',
]
);
The full namespace needs to be passed separately:
<?php
$router->add(
'/login',
[
'namespace' => 'Admin\Controllers',
'controller' => 'login',
'action' => 'index',
]
);
HTTP Methods¶
When you add a route using simply add()
, the route will be enabled for any HTTP method. Sometimes we can restrict a route to a specific method. This is particularly useful when creating RESTful applications.
<?php
// GET
$router->addGet(
'/invoices/edit/{id}',
'Invoices::edit'
);
// POST
$router->addPost(
'/invoices/save',
'Invoices::save'
);
// POST/PUT
$router->add(
'/invoices/update',
'Invoices::update'
)->via(
[
'POST',
'PUT',
]
);
Converters¶
Converters are snippets of code that allow you to convert the parameters of a route prior to it being sent to the dispatcher
<?php
$route = $router->add(
'/products/{slug:[a-z\-]+}',
[
'controller' => 'products',
'action' => 'show',
]
);
$route->convert(
'slug',
function ($slug) {
return str_replace('-', '', $slug);
}
);
/products/new-ipod-nano-generation
. The convert
method will change the parameter to newipodnanogeneration
. Another use case for converters is when binding a model to a route. This allows the model to be passed into the defined action directly.
<?php
$route = $router->add(
'/products/{id}',
[
'controller' => 'products',
'action' => 'show',
]
);
$route->convert(
'id',
function ($id) {
return Product::findFirstById($id);
}
);
Groups¶
If a set of routes have common paths they can be grouped for easier maintenance. To achieve this, we utilize the Phalcon\Mvc\Router\Group component
<?php
use Phalcon\Mvc\Router;
use Phalcon\Mvc\Router\Group;
$router = new Router();
$invoices = new RouterGroup(
[
'module' => 'admin',
'controller' => 'invoices',
]
);
$invoices->setPrefix('/invoices');
$invoices->add(
'/list',
[
'action' => 'list',
]
);
$invoices->add(
'/edit/{id}',
[
'action' => 'edit',
]
);
$invoices->add(
'/view',
[
'controller' => 'common',
'action' => 'index',
]
);
$router->mount($invoices);
/invoices
. We then add more routes to the group, some without parameters and some with. The last route allows us to use a different controller than the default one (common
). Finally, we add the group to the router. We can extend the Phalcon\Mvc\Router\Group component and register our routes in it on a per-group basis. This allows us to better organize the routes of our application.
<?php
use Phalcon\Mvc\Router\Group;
class InvoicesRoutes extends Group
{
public function initialize()
{
$this->setPaths(
[
'module' => 'invoices',
'namespace' => 'Invoices\Controllers',
]
);
$this->setPrefix('/invoices');
$this->add(
'/list',
[
'action' => 'list',
]
);
$this->add(
'/edit/{id}',
[
'action' => 'edit',
]
);
$this->add(
'/view',
[
'controller' => 'common',
'action' => 'index',
]
);
}
}
Now we can mount the custom group class in the router:
Matching Routes¶
A valid URI must be passed to the Router so that it can process it and find a matching route. By default, the routing URI is taken from the $_GET['_url']
variable that is created by the rewrite engine module. A couple of rewrite rules that work very well with Phalcon are:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]
In this configuration, any requests to files or folders that do not exist will be sent to index.php
. The following example shows how to use this as a stand-alone component:
<?php
use Phalcon\Mvc\Router;
$router = new Router();
// ...
$router->handle(
$_GET["_url"]
);
echo $router->getControllerName();
echo $router->getActionName();
$route = $router->getMatchedRoute();
_url
element from the $_GET
superglobal and after that, we can get the controller name or the action name or even get back the matched route. Naming Routes¶
Each route that is added to the router is stored internally as a Phalcon\Mvc\Router\Route object. That class encapsulates all the details of each route. For instance, we can give a name to a path to identify it uniquely in our application. This is especially useful if you want to create URLs from it.
<?php
$route = $router->add(
'/admin/{year:[0-9]{4}}/{month:[0-9]{2}}/{day:[0-9]{2}}/{id:[0-9]{4}',
'Invoices::view'
);
$route->setName('invoices-view');
Then, using for example the component Phalcon\Url we can build routes from the defined name:
<?php
// /admin/2019/12/25/1234
echo $url->get(
[
'for' => 'invoices-view',
'year' => '2019',
'month' => '12',
'day' => '25',
'id' => '1234',
]
);
Default Behavior¶
Phalcon\Mvc\Router has a default behavior providing simple routing that always expects a URI and matches the following pattern:
For example, for a URL like this https://dev.phalcon.od/download/linux/ubuntu.html
, this router will translate it as follows:
Controller | Action | Parameter |
---|---|---|
DownloadController | linuxAction | ubuntu.html |
If you do not want the router to follow this behavior, you must create the router passing false
in the constructor.
Default Route¶
When your application is accessed without any route, the /
route is used to determine what paths must be used to show the initial page in your application
Not Found (404)¶
If none of the routes, specified in the router, match, you can define a 404 controller/action by using the notFound
method.
NOTE
This will only work if the router was created without default routes: $router = Phalcon\Mvc\Router(false);
Defaults¶
You can define default values for module
, controller
, and `action. When a route is missing any of these elements in its path, the router will automatically use the default value set.
<?php
$router->setDefaultModule('admin');
$router->setDefaultNamespace('Admin\Controllers');
$router->setDefaultController('index');
$router->setDefaultAction('index');
$router->setDefaults(
[
'controller' => 'index',
'action' => 'index',
]
);
Trailing Slashes¶
Sometimes a route could be accessed with extra/trailing slashes. The extra slashes will produce a not-found status in the dispatcher, which is not what we want. You can set up the router to automatically remove the slashes from the end of the handled route.
Or, you can modify specific routes to optionally accept trailing slashes:
<?php
$route = $router->add(
'/admin/:controller/status[/]{0,1}',
[
'controller' => 2,
'action' => 'status',
]
);
[/]{0,1}
allows for an optional trailing slash Callbacks¶
Sometimes, routes should only be matched if they meet specific conditions. You can add arbitrary conditions to routes using the beforeMatch
callback. If this function returns false
, the route will be treated as non-matched:
<?php
$route = $router->add(
'/login',
[
'module' => 'admin',
'controller' => 'session',
]
);
$route->beforeMatch(
function ($uri, $route) {
if (true === isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
$_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest'
) {
return false;
}
return true;
}
);
false
if it was not You can create a filter class, to allow you to inject the same functionality in different routes.
<?php
class AjaxFilter
{
public function check()
{
return $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest';
}
}
To set this up, we just add the class to the beforeMatch
call.
<?php
$route = $router->add(
'/login',
[
'module' => 'admin',
'controller' => 'session',
]
);
$route->beforeMatch(
[
new AjaxFilter(),
'check'
]
);
Finally, you can use the beforeMatch
method (or event) to check whether this was an AJAX call or not.
<?php
use Phalcon\Di\DiInterface;
use Phalcon\Http\Request;
use Phalcon\Mvc\Router\Route;
$route = $router->add(
'/login',
[
'module' => 'admin',
'controller' => 'session',
]
);
$route->beforeMatch(
function ($uri, $route) {
/**
* @var string $uri
* @var Route $route
* @var DiInterface $this
* @var Request $request
*/
$request = $this->getShared('request');
return $request->isAjax();
}
);
Hostname¶
The Phalcon\Mvc\Router component also allows for hostname constraints. This means that the specific routes or a group of routes can be restricted to only match the route if it originated from a specific hostname.
<?php
$route = $router->add(
'/admin/invoices/:action/:params',
[
'module' => 'admin',
'controller' => 'invoices',
'action' => 1,
'params' => 2,
]
);
$route->setHostName('dev.phalcon.ld');
The hostname can also be passed as a regular expression:
<?php
$route = $router->add(
'/admin/invoices/:action/:params',
[
'module' => 'admin',
'controller' => 'invoices',
'action' => 1,
'params' => 2,
]
);
$route->setHostName('([a-z]+).phalcon.ld');
When using groups of routes, you can set the hostname constraints that apply to every route in the group.
<?php
use Phalcon\Mvc\Router\Group;
$invoices = new Group(
[
'module' => 'admin',
'controller' => 'invoices',
]
);
$invoices->setHostName('dev.phalcon.ld');
$invoices->setPrefix('/invoices');
$invoices->add(
'/',
[
'action' => 'index',
]
);
$invoices->add(
'/list',
[
'action' => 'list',
]
);
$invoices->add(
'/view/{id}',
[
'action' => 'view',
]
);
$router->mount($invoices);
Testing¶
This component does not have any dependencies. As such you can create unit tests to test your routes.
<?php
use Phalcon\Mvc\Router;
$testRoutes = [
'/',
'/index',
'/index/index',
'/index/test',
'/products',
'/products/index/',
'/products/show/101',
];
$router = new Router();
foreach ($testRoutes as $testRoute) {
// Handle the route
$router->handle($testRoute);
echo 'Testing ', $testRoute, '<br>';
// Check if some route was matched
if ($router->wasMatched()) {
echo 'Controller: ', $router->getControllerName(), '<br>';
echo 'Action: ', $router->getActionName(), '<br>';
} else {
echo "The route wasn't matched by any route<br>";
}
echo '<br>';
}
Events¶
Similar to other Phalcon components, Phalcon\Mvc\Router also has events, when an Events Manager is present. The available events are:
Event | Fired when |
---|---|
afterCheckRoutes | After checking all the routes |
beforeCheckRoute | Before checking a route |
beforeCheckRoutes | Before checking all loaded routes |
beforeMount | Before mounting a new route |
matchedRoute | When a route is matched |
notMatchedRoute | When a route is not matched |
Annotations¶
This component provides a variant that is integrated with the annotations service. Using this strategy you can write the routes directly in the controllers instead of adding them to router component directly.
<?php
use Phalcon\Mvc\Router\Annotations;
$container['router'] = function () {
$router = new Annotations(false);
$router->addResource('Invoices', '/admin/invoices');
return $router;
};
false
to remove the default behavior. After that, we are instructing the component to read the annotations from the InvoicesController
if the URI matches /admin/invoices
. The InvoicesController
will need to have the following implementation:
<?php
/**
* @RoutePrefix('/admin/invoices')
*/
class InvoicesController
{
/**
* @Get(
* '/'
* )
*/
public function indexAction()
{
}
/**
* @Get(
* '/edit/{id:[0-9]+}',
* name='invoice-edit'
* )
*/
public function editAction($id)
{
}
/**
* @Route(
* '/save',
* methods={'POST', 'PUT'},
* name='invoice-save'
* )
*/
public function saveAction()
{
}
/**
* @Route(
* '/delete/{id:[0-9]+}',
* methods='DELETE',
* converters={
* id='MyConverters::checkId'
* }
* )
*/
public function deleteAction($id)
{
}
}
Only methods marked with valid annotations are used as routes. The available annotations are:
Annotation | Description | Usage |
---|---|---|
Delete | Restrict the HTTP method to DELETE | @Delete('/invoices/delete/{id}') |
Get | Restrict the HTTP method to GET | @Get('/invoices/search') |
Options | Restrict the HTTP method to OPTIONS | @Option('/invoices/info') |
Post | Restrict the HTTP method to POST | @Post('/invoices/save') |
Put | Restrict the HTTP method to PUT | @Put('/invoices/save') |
Route | Mark a method as a route. Must be placed in a method docblock | @Route('/invoices/show') |
RoutePrefix | Prefix to be prepended to each route URI. Must be placed in the class docblock | @RoutePrefix('/invoices') |
For annotations that add routes, the following parameters are supported:
Name | Description | Usage |
---|---|---|
converters | A hash of converters for the parameters | @Route('/posts/{id}/{slug}', converter={id='MyConverter::getId'}) |
methods | One or more HTTP methods allowed for the route | @Route('/api/products', methods={'GET', 'POST'}) |
name | The name for the route | @Route('/api/products', name='get-products') |
paths | Paths array for the route | @Route('/invoices/view/{id}/{slug}', paths={module='backend'}) |
If you are using modules in your application, it is better to use the addModuleResource()
method:
<?php
use Phalcon\Mvc\Router\Annotations;
$container['router'] = function () {
$router = new Annotations(false);
$router->addModuleResource(
'admin',
'Invoices',
'/admin/invoices'
);
return $router;
};
Admin\Controllers\InvoicesController
if the URI starts with /admin/invoices
. The router also understands prefixes to ensure that the routes are resolved as fast as possible. For instance for the following routes:
only the /clients
prefix can be used in all controllers, thus speeding up the lookup.
Dependency Injection¶
You can register the router component during the container setup, to make it available inside the controllers or any other components that extend the Phalcon\Di\Injectable component.
You can use the example below in your bootstrap file (for example index.php
or app/config/services.php
if you use Phalcon Developer Tools).
<?php
$container->set(
'router',
function () {
require __DIR__ . '/app/config/routes.php';
return $router;
}
);
You need to create app/config/routes.php
and add the router initialization code:
<?php
use Phalcon\Mvc\Router;
$router = new Router();
$router->add(
'/login',
[
'controller' => 'login',
'action' => 'index',
]
);
$router->add(
'/invoices/:action',
[
'controller' => 'invoices',
'action' => 1,
]
);
return $router;
Custom¶
You can create your own components by implementing the supplied interfaces: - Phalcon\Mvc\Router\GroupInterface - Phalcon\Mvc\Router\RouteInterface - Phalcon\Mvc\RouterInterface
Examples¶
The following are examples of custom routes:
<?php
// '/system/admin/a/edit/7001'
$router->add(
'/system/:controller/a/:action/:params',
[
'controller' => 1,
'action' => 2,
'params' => 3,
]
);
// '/en/news'
$router->add(
'/([a-z]{2})/:controller',
[
'controller' => 2,
'action' => 'index',
'language' => 1,
]
);
// '/en/news'
$router->add(
'/{language:[a-z]{2}}/:controller',
[
'controller' => 2,
'action' => 'index',
]
);
// '/admin/posts/edit/100'
$router->add(
'/admin/:controller/:action/:int',
[
'controller' => 1,
'action' => 2,
'id' => 3,
]
);
// '/posts/2015/02/some-cool-content'
$router->add(
'/posts/([0-9]{4})/([0-9]{2})/([a-z\-]+)',
[
'controller' => 'posts',
'action' => 'show',
'year' => 1,
'month' => 2,
'title' => 3,
]
);
// '/manual/en/translate.adapter.html'
$router->add(
'/manual/([a-z]{2})/([a-z\.]+)\.html',
[
'controller' => 'manual',
'action' => 'show',
'language' => 1,
'file' => 2,
]
);
// /feed/fr/hot-news.atom
$router->add(
'/feed/{lang:[a-z]+}/{blog:[a-z\-]+}\.{type:[a-z\-]+}',
'Feed::get'
);
// /api/v1/users/peter.json
$router->add(
'/api/(v1|v2)/{method:[a-z]+}/{param:[a-z]+}\.(json|xml)',
[
'controller' => 'api',
'version' => 1,
'format' => 4,
]
);
NOTE
Be careful when allowing characters in regular expressions for controllers and namespaces. These will become class names and in turn, they will interact with the file system. As such, it is possible that an attacker can access unauthorized files. A safe regular expression is: /([a-zA-Z0-9\_\-]+)