You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By default, the router executes all routes that match. You could say it combines the defined routes with AND.
But as each route definition returns whether the route matches, you can change it so that the router executes only the first matching route.
Instead of the default
$router->get('/', function () {
// do something
});
just write
$router->get('/', function () {
// do something
}) && exit;
to combine them with OR instead.
If you do that, you can simply include your 404 page after all route definitions. It will only be shown if no route matched.
This is available as of today. Alternatively, we could include a new method like Router#didAnyRouteMatch in the future. With that method, you could check if the request has already been handled or if you should show your 404 page instead.
Has the Router#didAnyRouteMatch function already implemented? It would be great, I'm currently using this, and it works fine for me but it would be much better with a function that determines if any routes have been loaded or not:
$router->get('/', function () {
// do something
}) && $find = true;
Activity
ocram commentedon Dec 4, 2019
Thanks, good question!
By default, the router executes all routes that match. You could say it combines the defined routes with
AND
.But as each route definition returns whether the route matches, you can change it so that the router executes only the first matching route.
Instead of the default
just write
to combine them with
OR
instead.If you do that, you can simply include your 404 page after all route definitions. It will only be shown if no route matched.
This is available as of today. Alternatively, we could include a new method like
Router#didAnyRouteMatch
in the future. With that method, you could check if the request has already been handled or if you should show your 404 page instead.Does that help?
brahimbjz commentedon May 18, 2020
Has the
Router#didAnyRouteMatch
function already implemented? It would be great, I'm currently using this, and it works fine for me but it would be much better with a function that determines if any routes have been loaded or not:ocram commentedon May 18, 2020
That feature is indeed not available yet. Sorry for that!
In the meantime, the solution we proposed above
or your solution
should both work fine, although admittedly being a little more verbose.