Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

[Proposal] Route::fallback(); #653

Closed
Evertt opened this issue Jun 26, 2017 · 6 comments
Closed

[Proposal] Route::fallback(); #653

Evertt opened this issue Jun 26, 2017 · 6 comments

Comments

@Evertt
Copy link

Evertt commented Jun 26, 2017

Basically it would be nice to be able to define a fallback-route like so:

Route::fallback('PageController@someMethod');
// or
Route::fallback(function() {
    // code here...
});

Which would match any GET route that doesn't match any of the other routes. There can only be one fallback route and if more are defined then the last one always overwrites the previous one. Maybe, because of that, it would also be nice to be able to check whether one was already defined:

if (!Route::hasFallback()) {
    Route::fallback(...);
}

If more people like this then I'm willing to write a PR.

edit

Maybe it would actually be nice to have fallbacks for every http method. In that case in might be nice to be able to write this:

Route::fallbackResource('PageController');
// or maybe instead like this?
Route::fallbackController('SomeController'); // in which the method names should match the http verbs?

Or not?

@Evertt Evertt changed the title [Proposal] Route::fallback() [Proposal] Route::fallback(); Jun 27, 2017
@joshmanders
Copy link

Isn't that basically the same as Route::get('*', 'PagesController@someMethod'); at bottom of routes/web.php?

@Evertt
Copy link
Author

Evertt commented Jun 27, 2017

Almost yes. The problem with that is that routes and route files aren't always so straight-forward.

For example I'm working on a multi-tenant application where there are common routes and there are tenant-specific routes. And then I also have vendor packages which register routes. Because of that structure it's risky to put Route::get('*', ...); anywhere, because that means that any route which is defined after that one will not work anymore.

So when I write Route::fallback() I really mean to say "Only try this after all else failed."

@joshmanders
Copy link

Ah, I see now. Cool.

@garygreen
Copy link

This is a nice idea - though I don't think it's needed for every resource. This could solve #413
Instead of throwing 404 not founds just have the router match the route as a fallback sending a 404 response. Maybe have a built in one for Laravel PageController which sends a 404 response with ability to override the fallback with your own?

@themsaid
Copy link
Member

themsaid commented Sep 18, 2017

Check laravel/framework#21234

@carestad
Copy link

Would it be possible to have a Route::fallbackView() as well, similar to Route::view()? See #1961 regarding why.

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

No branches or pull requests

5 participants