Please, sorry for my English :)
The Route::fallback() functionality does not cover all "404 Not Found" cases. See 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]+');
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.
But actually Laravel application shows the "405 (Method Not Allowed)" response.
:thinking:
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
I don't know the good solution, but I think that there are two simple solutions that I see:
what if define him for the other route methods too?...
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.
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:
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.