(1/1) MethodNotAllowedHttpException

Published 4 weeks ago by surabhi

Hii,

I am new to laravel.I am facing very strange problem.My post routes are not working properly,as I was continuously getting error Method not allowed exception.Please guide me.

web.php

Auth::routes(); Route::get('/check-token-data','[email protected]_tokens_view'); Route::post('/check-token-view','[email protected]_tokens_data');

Controller

Sergiu17
Sergiu17
4 weeks ago (164,630 XP)

When you submit your form, don't forget about csrf https://laravel.com/docs/5.6/csrf

<form>
{{ csrf_field() }}
</form>

or if you user 5.6+

<form>
@csrf
</form>
surabhi

hey thanks.But I have already used it,still not working

Sergiu17
Sergiu17
4 weeks ago (164,630 XP)
Route::post('/check-token-view', '[email protected]_tokens_data');

Make sure your form sends a POST request to this end point

<form method='post' action='/check-token-view'>
    @csrf
</form>
surabhi

yes I have used post method.

Sergiu17
Sergiu17
4 weeks ago (164,630 XP)
php artisan route:list

make sure you send request to /check-token-view// slash in front, post method

surabhi

Hii,

Actually my all forms are working fine on localhost machine,but as I hosted my website on server,then firstly error of Tokenmismatch error found,to solve this problem I have changed code in Exception/handler.php code so I found MethodNotAllowedHttpException error on all post method forms.

webtrickshome

Basically such errors appear due to typo mistakes which work fine on localhost but not on web servers. Would you clarify what changes did you make in handler.php file because it's not the one you are supposed to modify in most of the cases. Here's a project based tutorial on laravel which might help you.

surabhi
namespace App\Exceptions;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Session\TokenMismatchException;

class Handler extends ExceptionHandler {

/**
 * A list of the exception types that should not be reported.
 *
 * @var array
 */
protected $dontReport = [
    'Symfony\Component\HttpKernel\Exception\HttpException'
];
/**
 * Report or log an exception.
 *
 * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
 *
 * @param  \Exception  $e
 * @return void
 */
public function report(Exception $e)
{
    return parent::report($e);
}
/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $e
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $e)
{
    if ($e instanceof TokenMismatchException){
        // Redirect to a form. Here is an example of how I handle mine
        return redirect($request->fullUrl())->with('csrf_error',"Oops! Seems you couldn't submit form for a long time. Please try again.");
    }

    return parent::render($request, $e);
}

}

surabhi

above is the handler.php code

m7vm7v
m7vm7v
4 weeks ago (97,550 XP)

Try switching to named routes Route::post('/check-token-view','[email protected]_tokens_data')->name(check-token-view);

Then in the form action {{ route('check-token-view') }}

surabhi

I have tried it....still getting same error..

pardeepkumar
<form action="{{ route('users.update', ['slug' => $users.->slug ])}}" method="post">
    {{ csrf_field() }}
    {{ method_field('PUT') }}


Route::put('users/update/{slug}', [
  'uses' => '[email protected]',
  'as' => 'users.update'
  ]);

I think you are likely trying to access the endpoint as a GET but haven't set it up properly in your Laravel routes.

Route::get('oauth/token', '[email protected]');

surabhi

same error...

surabhi

It is very strange..all get methods are working but not post methods

m7vm7v
m7vm7v
4 weeks ago (97,550 XP)

Hi @surabhi Can you paste the full stack of the error message you see?

Please sign in or create an account to participate in this conversation.