-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.5] Add ability to set a fallback route #21234
Conversation
Just for completeness this was also proposed at PR #20923 (closed). Nice to see this feature request being considered again. 👍 |
@paulofreitas didn't know there was a PR already, this one is similar with the difference that it makes sure your fallbacks URL are sorted at the very end of the routes collection so that a custom wild-card route would match first before using the fallback route. |
So what is different about this vs just overriding the 404 handling?
|
@laurencei in error views you don't have access to sessions, basically no middleware runs. |
Arh yep, of course 😃 |
I also liked #20923 a lot. I've been using it since that PR was created. |
@@ -189,7 +189,9 @@ public function match(Request $request) | |||
*/ | |||
protected function matchAgainstRoutes(array $routes, $request, $includingMethod = true) | |||
{ | |||
return Arr::first($routes, function ($value) use ($request, $includingMethod) { | |||
return collect($routes)->sort(function ($route) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bit of an open-ended comment: sort
takes two parameters to compare and, iirc, ignoring the second one (by not properly returning 1 or -1) can result in what appears to be a somewhat randomized order.
Perhaps you should do $routeA->isFallback <=> $routeB->isFallback
? I'm going to try and write a test to verify this theory (so such a change could be caught in the future)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spaceship!
@themsaid Thanks |
Look like the fallback() method will only result in GET and HEAD methods. |
This was requested in:
The idea is to have a catch-all Route that you can use as a fallback if the request didn't match any of the existing routes, so for example:
dashboard/non-existing
will throw a 404 and render the 404 view if no fallback route is used.Route::fallback(function(){ return response()->view('dashboard.404', [], 404); })
the dashboard specific 404 page will be displayed.In the argument you pass to fallback() you can put a closure or a controller call
DashboardController@notFound
, this will allow you to render a Not Found page with the request passing through all middleware so you have sessions, cookies, etc... available.So for example in Forge, if a logged in user hit a wrong URL we can display a 404 view but he still has access to the header where he can browse to any of the existing screens.