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.

Replies

You are reading an archived discussion.

Related Posts

How do Laravel and WordPress compete with each other on the basis of Speed, Performance, Security and SEO? I was faced with this question about 1.5 years ago; and having...
Design News says: It’s hard to believe, but electrically powered flight has been around for 135 years. Lighter-than-air dirigibles in the 1880s were the first to use electric motors for...
I am a fresher just completed by BE in Electronics. 65%, and no job and at present I am in a dilemma of what to do further should i go...
Hello,I am thinking to release one gaming blog for my minor project. Would it be a good idea or no?I'm thinking to start mini militia health mod blog. This game...
im a second year student and wanted to know more about how to start a case study and a project.i have got many topics online but don't know what to...