Framework: The Route::fallback() functionality does not work for any requests with another method than "GET"

Created on 10 Sep 2018  路  1Comment  路  Source: laravel/framework

Please, sorry for my English :)

  • Laravel Version: 5.7.2 (and 5.6.~ too)
  • PHP Version: 7.2
  • Database Driver & Version: DB not used

Description:

The Route::fallback() functionality does not cover all "404 Not Found" cases. See steps to reproduce.

Steps To Reproduce:

First, create the "fallback" route in web.php file:

Route::get('/', function () {
    return 'Welcome!';
});

Route::fallback(function() {
    return response()->json(['message' => 'Not Found.'], 404);
});

When you enter in browser something like "/abcde" the Laravel shows the proper 404 response. But...

Second, add the simple route with PUT method into api.php file:

Route::put('/orders/{order_id}', function () {
    return 'Updated.';
})->where('order_id', '[0-9]+');

Expect

When you try to send a request to "/orders/abcde" you expect that the Laravel application returns the "404 (Not Found)" response as we defined in fallback route in web.php.

Actual

But actually Laravel application shows the "405 (Method Not Allowed)" response.

:thinking:

Why Laravel does this?

I found that when the Laravel does not found any route, the Laravel searches any route with other request methods that can be suitable for the current request:
https://github.com/laravel/framework/blob/9f313ce9bb5ad49a06ae78d33fbdd1c92a0e21f6/src/Illuminate/Routing/RouteCollection.php#L173-L177

Then this code will be executed:
https://github.com/laravel/framework/blob/9f313ce9bb5ad49a06ae78d33fbdd1c92a0e21f6/src/Illuminate/Routing/RouteCollection.php#L234-L242

  • and because the request method is "PUT" (not "OPTIONS") the Laravel throws MethodNotAllowedHttpException.

How this problem can be solved?

I don't know the good solution, but I think that there are two simple solutions that I see:

  1. When we define the "fallback" route, he is defined only for the "GET" requests:
    https://github.com/laravel/framework/blob/544b75943c055ec7e6318cb00ec53d5cc8661aa5/src/Illuminate/Routing/Router.php#L222-L229
  2. what if define him for the other route methods too?...

  3. When the Laravel searches the suitable routes for the current request, I think that the Laravel should exclude the "fallback" routes from the all routes collection.

>All comments

As this PR was declined by Taylor, I'll close the issue here.

Instead please try asking your question on one of the many great community support areas that will likely give you a better answer more quickly:

  • Laravel Slack (https://larachat.co/)
  • Laravel.io Forum (https://laravel.io/forum)
  • Laracasts Forum (https://laracasts.com/discuss)
  • StackOverflow (http://stackoverflow.com/questions/tagged/laravel)

If you feel I've closed this issue in error, please provide more information about how this is a framework issue, and I'll reopen the ticket.

Thanks in advance.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Fuzzyma picture Fuzzyma  路  3Comments

JamborJan picture JamborJan  路  3Comments

RomainSauvaire picture RomainSauvaire  路  3Comments

CupOfTea696 picture CupOfTea696  路  3Comments

felixsanz picture felixsanz  路  3Comments