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.8] Support fallback routes for different methods #25555

Closed
wants to merge 1 commit into from
Closed

[5.8] Support fallback routes for different methods #25555

wants to merge 1 commit into from

Conversation

staudenmeir
Copy link
Contributor

Route::fallback() currently only supports GET (and HEAD) requests.

With this PR, people can use one fallback route for multiple HTTP methods:

Route::fallback('FallbackController@fallback', ['GET', 'POST']);

Or they can specify different fallback routes:

Route::fallback('FallbackController@fallbackGet');
Route::fallback('FallbackController@fallbackPost', 'POST');

Fixes #25550.

@taylorotwell
Copy link
Member

I don't see the point of this tbh.

@ilnurshax
Copy link

ilnurshax commented Sep 11, 2018

@taylorotwell Hello! You say that "don't see the point of this", but how can I show the "fallback" response for the other requests than "GET"?
How can the developers handle the use cases as described in #25550 ?

@BrandonSurowiec
Copy link
Contributor

You can use this in your route file:

Route::any('{catchall}', 'FallbackController@index')
    ->where('catchall', '.*')
    ->fallback(); 

@sobhanatar
Copy link

I don't see the point of this tbh.

@taylorotwell
Using regular expression in routes is something that let us to do as much its possible. But I think the idea behind the fallback is to make the code clear and readable as much as possible.

For example if I need to log user action to access non-existent methods I have to do logging in two different places:

  1. The fallback for methods with GET verb
  2. Checking Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException in Handler.php to log non-existence routes that are not exists in my application.

So maybe it's better that fallback method by default wait for any verb.

@gilbertoalbino
Copy link

@staudenmeir I can imagine how frustrated you may have got as @taylorotwell didn't give you any further explanation, but for those coming later to this issue:

In app/Exceptions/Handlers.php

you can do so:

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class Handler extends ExceptionHandler
{
    public function render($request, Throwable $exception)
    {
        if ($exception instanceof NotFoundHttpException) {
            return response('', Response::HTTP_NOT_FOUND);
        }

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

@jvdyck
Copy link

jvdyck commented Oct 13, 2020

Please at least consider updating the documentation so that it states that fallback only supports GET/HEAD requests...

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.

7 participants