Framework: Route caching results in 405 Method Not Allowed for root path

Created on 15 May 2020  路  7Comments  路  Source: laravel/framework


  • Laravel Version: 7.11.0
  • PHP Version: 7.3.5
  • Database Driver & Version: N/A

EDIT (platform sepcs):

  • Platform: Windows 10
  • Apache 2.4.39 (using WAMPP 3.1.9)

Description:

It appears that as soon as you call php artisan route:cache, the GET / route returns a 405 Method Not Allowed error. I originally encountered this in a running project, but after updating all packages, the issue still persisted.

I've browsed through the issues and there appears to have been done some work related to routing, mainly https://github.com/laravel/framework/issues/32228 , https://github.com/laravel/framework/issues/31756 and https://github.com/laravel/framework/issues/31768 , however I couldnt track down any issue that could remain.

Steps To Reproduce:

I can reproduce it using a fresh Laravel project:

  • composer global require laravel/installer
  • laravel new laravel-route-bug
  • Go to the directory
  • Run composer install
  • Remove the single route from routes/api.php (not cacheable because closure)
  • Change the route in routes/web.php to:

    • Route::get('/', ['uses' => 'Controller@index', 'as' => 'index.index']);

  • Add this function to the root controller in app\Http\Controllers\Controller.php:
public function index() {
    return 'this is index';
}
  • Create .env copy from .env.example and call php artisan key:generate
  • Call php artisan route:cache (should execute successfully now)
  • Go to the base url of your project in your browser. You should encounter the error message:

    • The GET method is not supported for this route. Supported methods: HEAD.

Most helpful comment

@bert-w we don't support Laravel in a subdirectory: https://laravel.com/docs/7.x/installation#directory-configuration

Screenshot 2020-05-16 at 12 59 50

All 7 comments

reproduction works right here! you might be making a simple mistake!

or this might not be a framework issue!

I followed your steps and can't recreate it either. Will ping @driesvints just in case.

I have some additional information which I encountered during testing:

I always develop my applications using simple http://localhost/someproject/public URLs. This has always worked perfectly (at least in v6.x, it could be that I just haven't encountered this issue since v7.0.0).

I notice that the issue is gone when I create a vhost for laravel-project.loc and point it to the correct directory, and then call the application using http://laravel-project.loc.

For what it's worth, here's a gist of the route file that is being generated:
https://gist.github.com/bert-w/1ea42bb124de4b857d62f04d0ad856aa

I tried tracing down the issue through xdebug, but it sent me far into the Symfony routing package, to be exact here: https://github.com/symfony/routing/blob/master/Matcher/Dumper/CompiledUrlMatcherTrait.php#L90

This line tries to find the correct route in the compiled routes file app/bootstrap/cache/routes-v7.php (see the gist above), but it does so using the variable $trimmedPathinfo = '/someproject/public' (which results in no matches, since it has to be / to be able to find the correct route).

I wish I knew where to look to find the issue; there's just a lot to go through.

@bert-w we don't support Laravel in a subdirectory: https://laravel.com/docs/7.x/installation#directory-configuration

Screenshot 2020-05-16 at 12 59 50

Thanks, I didnt know about that restriction. Something did change between 6.x and 7.x that caused this to break though. Quite possibly one of the updated Symfony modules.

It's just a minor issue then if this only affects a development environment. I can live with not-executing php artisan route:cache on my local environment :)

If it helps @bert-w - We hit this on select environments during upgrades to L7. We tracked it down to a trailing slash on url. Which makes sense with the comments about subdirectories.

Some environments had like

foo.example.com/

and the working ones

foo.example.com

So the URL would become

foo.example.com//api/bar

and die out

{
    "message": "The POST method is not supported for this route. Supported methods: GET, HEAD."
}

I imagine something in newer Symfony is a bit more strict, but boy that sent me on a search.

I can confirm I have the same problem and website is under http://my-domain/apps123 who is redirected to http://my-domain/apps123/

Please try to reproduce with sub folder domain, and let us know if it works or not.

Also I confirm if you don't remove the single route from routes/api.php (not cacheable because closure) it works fine because the cache is not enabled, but also then it falls under this issues:

So in our case the problem is definitively with the caching aka artisan route:cache

EDIT:
I found a solution to bypass the problem. If you add the route "/apps123" it works with the caching.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments

YannPl picture YannPl  路  3Comments

lzp819739483 picture lzp819739483  路  3Comments

kerbylav picture kerbylav  路  3Comments

Anahkiasen picture Anahkiasen  路  3Comments