Skip to content
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

Merged
merged 5 commits into from
Sep 19, 2017
Merged

[5.5] Add ability to set a fallback route #21234

merged 5 commits into from
Sep 19, 2017

Conversation

themsaid
Copy link
Member

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.
  • If we use 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.

@paulofreitas
Copy link
Contributor

Just for completeness this was also proposed at PR #20923 (closed). Nice to see this feature request being considered again. 👍

@themsaid
Copy link
Member Author

@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.

@laurencei
Copy link
Contributor

laurencei commented Sep 18, 2017

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 what is different about this vs just overriding the 404 handling?

public function render($request, Exception $e)
{ 
    if ($e instanceof NotFoundHttpException) {
        if (Auth::user()) {
            return response()->view('errors.404.user', [], 404);
        } else {
            return response()->view('errors.404.guest, [], 404);
        }
    }

    return parent::render($request, $e);
}

@themsaid
Copy link
Member Author

@laurencei in error views you don't have access to sessions, basically no middleware runs.

@laurencei
Copy link
Contributor

Arh yep, of course 😃

@deleugpn
Copy link
Contributor

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) {
Copy link
Contributor

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)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spaceship!

@taylorotwell taylorotwell merged commit b1e01e7 into laravel:5.5 Sep 19, 2017
@decadence
Copy link
Contributor

@themsaid Thanks

@zzal
Copy link

zzal commented Mar 23, 2018

Look like the fallback() method will only result in GET and HEAD methods.
Is there a way to include POST and DELETE?

@staudenmeir
Copy link
Contributor

@zzal See #25555.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants