For plugins it's quite common to use the syntax below.
kirby()->routes(array(
array(
'pattern' => 'my/awesome/(:any)',
'action' => function($uid) {
echo $uid;
}
)
));
- Developer guide - Routing
c::set
- Developer guide - Extension registry - Route
$kirby->set
- Toolkit - Routing
$router->register
Instead of use echo
or return false;
to output or return something from a route, you can create a response object.
return new Response('Hello World!', 'html', 200);
return new Response('You have an error!', 'html', 404);
return new Response(json_encode(['Hello' => 'World']), 'json', 200);
return new Response(snippet('error-message', [], true), 'html', 404);
The syntax site()->visit('error')
will not work if you change c::set('error')
. The example below is better in that case.
return site()->visit(site()->errorPage());
kirby()->routes(array(
array(
'pattern' => 'some/page',
'action' => function() {
return new Response('You have an error!', 'html', 404);
}
)
));
'pattern' => '(.+)', // Match all
'pattern' => '(?!assets)(.*)', // Match all except the assets folder
kirby()->routes(array(
array(
'pattern' => '(.+)',
'action' => function($uri) {
echo $uri;
}
)
));
- Github - #368 - Route with the pattern just
(:all)
does not match - Forum - Routes - Prevent nesting from hell
- Forum - Not condition in routes
'(:num)' => '(-?[0-9]+)',
'(:alpha)' => '([a-zA-Z]+)',
'(:any)' => '([a-zA-Z0-9\.\-_%=]+)',
'(:all)' => '(.*)',
'/(:num?)' => '(?:/([0-9]+)',
'/(:alpha?)' => '(?:/([a-zA-Z]+)',
'/(:any?)' => '(?:/([a-zA-Z0-9\.\-_%=]+)',
'/(:all?)' => '(?:/(.*)',