-
Notifications
You must be signed in to change notification settings - Fork 0
Route Parameters
Joshua Parker edited this page Dec 23, 2020
·
3 revisions
Parameters can be defined on routes using the {keyName}
syntax. When a route matches a contained parameter, an instance of the RouteParams
object is passed to the action.
use Qubus\Routing\Route\RouteParams;
$router->map(['GET'], 'posts/{id}', function($id) {
return $id;
});
If you need to add constraints to a parameter, you can pass a regular expression pattern to the where()
method of the defined Route
:
$router->map(['GET'], 'post/{id}/comment/{commentKey}', function ($id, $commentKey) {
return $id;
})->where(['id', '[0-9]+'])->where(['commentKey', '[a-zA-Z]+']);
Sometimes you may want to use optional route parameters. To achieve this, you can add a ?
after the parameter name:
$router->map(['GET'], 'posts/{id?}', function($id) {
if (isset($id)) {
// Parameter is set.
} else {
// Parameter is not set.
}
});