Methodnotallowedhttpexception in Routecollection.php - Laravel 5.2

Asked

Viewed 3,109 times

2

Developing an application using Laravel 5.2, PHP7.0, Apache2 and Centos 7, I am having the following error: Methodnotallowedhttpexception.

On the local server it works normally... when I publish on the production server the error occurs, follow the screen:

inserir a descrição da imagem aqui

Routes: inserir a descrição da imagem aqui

I’m trying to reach (POST) the URL: /es/create

In the route archive:

Route::group(['prefix' => 'es'], function()
{
    Route::group(['middleware' => 'auth'], function()
    {
        Route::post('create', 'SearchController@create');
    });
});

View:

<h1>Create</h1>
<form method="POST" action="/es/create/" enctype="application/x-www-form-urlencoded">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <input type="text" name="index">
    <button type="submit">CREATE</button>        
</form>

I didn’t put the controller here because it doesn’t even get to the controller... stops on course.

I’ve tried without the enctype="application/x-www-form-urlencoded", according to some posts... but it didn’t work.

Someone would have a suggestion?

Thank you!

P.S.: I found several posts in English, but none with the solution I need.

  • Usually errors related to Methodnotallowedhttpexception give because of the program logic that is wrong. You have to correctly separate the POST and GET in your route file.

  • @Falion thanks for the comment, however, didn’t help much... in the face of what you see that would be wrong? Because I’m at the beginning of development and it’s actually working on the local machine, which, in my opinion, the logic of the system is correct. Any comment on the code itself? At night I will do some tests on the server, because there are other systems running with Post enabled in Apache... But I’m finding the error very strange. Thanks for the comment again.

2 answers

3


The problem is in View, to action tag form shall indicate the complete route path:

Where are you like this:

action="/es/create"

Should be like this:

action="{{ url('/es/create') }}"

Follow full code corrected:

<h1>Create</h1>
<form method="POST" action="{{ url('/es/create/') }}" enctype="application/x-www-form-urlencoded">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <input type="text" name="index">
    <button type="submit">CREATE</button>        
</form>

-1

Another alternative would be you use the helper action, below this your changed code.

Instead:

<input type="hidden" name="_token" value="{{ csrf_token() }}">

Do it:

{{ csrf_field() }}

And here’s your changed code:

<h1>Create</h1>
<form method="POST" action="{{ action('SearchController@create') }}" enctype="application/x-www-form-urlencoded">
    {{ csrf_field() }}
    <input type="text" name="index">
    <button type="submit">CREATE</button>        
</form>
  • Hello, thank you for the reply. The result is the same in this case and that was not the point of the question. For it was not a mistake in the token, but in the method.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.