Build routes for a small project
- Mistrfilda
- Member | 76
I havent used it standalone, but there is a section in documentation about it https://doc.nette.org/…tion/routing#…
- Marek Bartoš
- Nette Blogger | 1275
It does only route matching. For dispatching you have to add an parameter to each route and based on this parameter decide yourself what to do. It can look like this:
$router = new RouteList();
$router->addRoute('/path', [
'controller' => function(): void {
// Do something
echo 'response';
},
]);
$httpRequest = (new Nette\Http\RequestFactory)->fromGlobals();
$parameters = $router->match($httpRequest);
if ($parameters === null) {
// no match, 404
exit();
}
$controller = $parameters['controller'] ?? null;
if($controller === null) {
throw new \Exception('each route has to define controller');
}
$controller();
exit();
Parameter controller
is a callback executed when route is
matched. You just have to make sure each route has this parameter. And never add
it to url (like
$router->addRoute('/path/<controller>',
) – this
parameter is definitely not safe to be changed by user.
Last edited by Marek Bartoš (2021-10-25 15:54)