Reputation: 137
I created a route for buy an item in laravel 5.7
Route::post('/buy/item', "userController@buy_item")->name('buy_item_form');
Everything works fine but when I refresh the page(replace to GET Request) I got an MethodNotAllowedHttpException. The GET route not exist, Its must return an 404 error. I dont understant why its return me this exception.
Upvotes: 0
Views: 257
Reputation: 2621
You are using a post, with post you have a @csrf token. when you click on refresh, you are doing a GET method instead of a post and for hence you get the method not allow exception. If you are not sending data you can change it to a get [Route::get] method.
If you want to accept the 2 methods [post,get] to have a better experience and manage the possible errors. You can accept the 2 methods on the route like:
Route::match(array('GET','POST'),'/buy/item', 'userController@buy_item')->name('buy_item_form');
And on the controller, define what to do base on the method.
if (Request::isMethod('get')){
// redirect user
}
if (Request::isMethod('post')){
// do logic for post method
}
Upvotes: 2