Laravel

How to Fix Laravel MethodNotAllowedHttpException


Introduction :

You are hitting a route in a Laravel project and you get MethodNotAllowedHttpException. This is actually not allowed by the HTTP method used for that route. For example, you are making a POST request to routes that are only designed to have GET requests.

Why This Happens

Laravel routes are strict about the HTTP method you use. You will receive this error when you define a route that returns a GET request but then send it a POST request because of a mismatched method.

How to Resolve

1. Check the Route: Simply navigate to routes/web.php or api.php and confirm that the route is defined with the right method. If for instance, you wish that route to accept a POST:

Route::post('/submit', 'FormController@submit');

 

2. Allow Multiple Methods: If the route is going to be handling GET and POST, you can use match:

Route::match(['get', 'post'], '/submit', 'FormController@submit');

3. Verify the Form: If this is happening with a form, double-check your HTML form has the proper method:

<form method="POST" action="/submit">

This error is all about ensuring the request method (GET, POST, etc.) is matching what you're defining in your routes file.

Conclusion :

This MethodNotAllowedHttpException occurs with a mismatch between the request method and what is defined in the route. Once you confirm that the method used by the request such as GET or POST is similar to the one defined in the routes, then it's very easy to avoid this problem.


Angular

Related Center Of Excellence