5

I'm having an odd issue with a POST form in Laravel.

When sending a post request, my Laravel throws an MethodNotAllowedHttpException. Upon looking into the errormessage, I can see that Laravel thinks that my request is a GET request, which it is not.

When looking at both POST data and GET data of the errorpage, Laravel seems to think that they are both empty. This leaves me a bit confused, since it seems that some kind of redirect is going on, the HTTP_REFERER on the error is the page I'm posting from.

I've had this issue before, where making a named route solved my problem, but I'm making a simple CMS, so templates for a form is used, and it's not possible for me to use named routes, without allowing the user to use Blade syntax which is a bad idea.

My route is as follows (Simplified to a "Hello world"):

Route::post('/signup/add', function(){
    echo "Hello world"; 
});

http://pastebin.com/EsAeyHFx <- Full routes.php

http://pastebin.com/ByHdUFcK <- My form. Nothing fancy, only plain text/radiobuttons input. No html or anything special.

The even more strange part on this, is that I have another form (login form) that does not result in this behavior.

I have been looking at several other questions on StackOverflow, but they all seem to end up being a mistake of sending POST data to a GET route. This is not my case.

If I change the route from POST to GET, it works fine.

I've also tried to change the action of the form to GET and use the hidden field _method and set it post - No success.

Can someone tell me what is going on with this Exception and how to fix it?

ADDED: After some experimenting, I found out that when using 3rd party software (Like chrome extension Postman) and sending a POST request to the page, it works as inteded.

5
  • Try URL::to('/signup/add/') in the "action" attribute of the form instead of just "/signup/add". Depending upon folder structure of your application it might change.
    – Sameer
    Mar 29, 2015 at 8:49
  • The form is pure html, so the action should not be able to change. When inspecting the form, the action is still "/signup/add". And the form is a part of a template, so I can only use html, not Laravel or Blade (might not be entirely clear in the question)
    – Fizk
    Mar 29, 2015 at 9:12
  • Could you post the entire html source code of the form page? I think the problem is in this page. I have tried to build a simple form by using your code and it worked.
    – mininoz
    Mar 29, 2015 at 9:20
  • Try doing a normal POST request to the URL from any REST client like this and if it working the problem is with HTML page.
    – Sameer
    Mar 29, 2015 at 9:25
  • Thanks for your time - I have found the problem :) Making an answer now!
    – Fizk
    Mar 29, 2015 at 9:27

2 Answers 2

11

Ok, after some digging around, making forms in jsfiddle, using jQuery and so on, I found the problem!

My form has a trailing slash in the action attribute, which it supposedly is not allowed to.

The solutions was simply to change my code to action="/signup/add" instead of action="/signup/add/

Man, I feel stupid...

0
0

The MethodNotAllowedHttpException error means that the verb of the query is not consistent with the expected routes.

Do a php artisan route: list for listing routes and look in development tools

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.