Skip to content
Advertisement

Symfony2 app.php receiving POST request as GET app_dev.php works fine

So I’m creating a route that requires posting. The route is as follows:

my_route:
  path:     /myroute/login
  defaults: { _controller: "MyBundle:Default:login"}
  methods:  [POST]

So I use postman to hit app_dev.php/myroute/login and I get the correct response that I am looking for, currently the page just returns “hello world”. Now when I change postman to just hit app.php/myroute/login I get an error saying:

The server returned a "405 Method Not Allowed".

I’m really confused as to why it says method not allowed so I tailed the prod.log file and got the following:

Uncaught PHP Exception SymfonyComponentHttpKernelExceptionMethodNotAllowedHttpException: "No route found for "GET /myroute/login": Method Not Allowed (Allow: POST)"

I’m sending the requests in postman as POST requests but somehow symfony is only seeing them as GET requests. Again I can change the url to app_dev.php/myroute/login and hit send and it works. I thought it was a caching issue so I tried the following to clear the caches:

$ php app/console cache:clear --env=prod --no-debug

This still doesn’t clear my problem so I even removed all the files from app/cache/prod as well. I can’t find anyone having issues that are similar to this so I’m hoping someone can point me in the right direction. I also thought it might be

Advertisement

Answer

After some more digging I discovered what the issue was. So I thought that maybe it was postman that was sending the incorrect method so I googled around for that which lead to this

Postman sending POST as GET

which lead to a question about htaccess files:

Redirect POST htaccess

So first off I set postman to hit app.php/myroute/login and xdebug showed the request method as GET. I moved the .htaccess file and hit the same url and the request method showed up as POST. Instead of hitting that url I moved my .htaccess file back and pointed at just /myroute/login and everything lit up. This was an extremely annoying exercise so I hope that this will save someone else in the future looking for the same problem.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement