Hi @lukasbestle, this indeed seems to work with JSON (on Kirby 2.5.4). But, then again, I have another route registered in the same router that should return a page (not JSON), and then your code doesn’t work for that scenario for me?
Am I doing something wrong?
Code:
$router = new Router();
$router->register(array(
array(
'pattern' => [
'(:any)/news',
'(:any)/news/(:any)'
],
'method' => 'GET',
'filter' => 'auth',
'action' => function($lang) {}
),
array(
'pattern' => 'test',
'method' => 'GET',
'action' => function() {
return response::json(['this' => ['is', 'a', 'test']]);
}
)
));
if($route = $router->run()) {
$response = call($route->action(), $route->arguments());
die($response);
}
FYI: The first route works as expected when I remove die($response);
from your code.
– Edit:
I read up here, and now I understand why the first works without die()
-> it runs the filter and then the default Kirby CMS router takes over and does what it usually does because the action is empty. Right?
If so, is there any way to call the default router in the action of the first route to avoid die();
?
– Edit 2:
After thinking a bit further about it; in my scenario it wouldn’t make sense to use a custom router for this as I can do it all in the regular routes. (I thought I couldn’t use alternative methods nor filters in the regular kirby routes — which is obviously wrong as shown here)
I actually don’t see a scenario for using a custom router then? Is there?