05 Sep 2018

MethodNotAllowedHttpException in RouteCollection Error In Laravel - How to fix?

The MethodNotAllowedHttpException in RouteCollection Error In Laravel is perhaps the most common error that every new Laravel user faces. This error is typically triggered when the Laravel framework is unable to figure out the right method that you intend to trigger. 

In most of the cases, this error is triggered when your 'GET' and 'POST' requests aren't matched in the routes/web.php file. 

Your first job should be to find out what kind of request is your form submitting - is it a GET request or a POST request. Then head over to the routes/web.php file and check if your route definition is correct.

I did this mistake most often. I'd intend to make a POST request from my form, but in the route, I'd have something like -

Route::get('/path/', 'MyController@method');

It's quite possible that you are using another request 'DELETE', 'PUT', 'PATCH' etc. by defining it with something like - {{ method_field('DELETE') }} - so check your routes file for appropriate request type. 

Also check what kind of request are you making to load the form itself. When I was starting out, I'd not have a GET request to load the form; and that'd blow up my code. Those are stupid mistakes every developer makes. 

Another common mistake that we all make, not specifically related to MethodNotAllowedHttpException is that we tend to forget the CSRF protection in our forms. 

If you are using any of the latest versions of Laravel 5.6+, you could make use of the @csrf tag in your forms. It helps making sure that your application is protected from unwanted cross site request forgery attacks. 

A friendly tip from me - keep your routes and controller actions as simple as possible; often trying to stick to the REST convention. It may not look like a big deal; but it has help me avoid a lot of problems while writing code. 

If you are getting MethodNotAllowedHttpException in Laravel and are unable to figure out the reason, post your questions below. I'll try my best to help you out.

Be the first one to reply

Share this content on your social channels -

Only logged in users can reply.