Cannot access POST route in Laravel via browser

2019-09-16 22:14发布

问题:

Using Laravel 5.4 have this route in web.php

Route::post('foo/bar', function () {
    return 'Hello World';
});

but when I visit the http://localhost:8000/foo/bar URL in browser I have this error:

MethodNotAllowedHttpException in RouteCollection.php line 251: in RouteCollection.php line 251 at RouteCollection->methodNotAllowed(array('POST')) in RouteCollection.php line 238 at RouteCollection->getRouteForMethods(object(Request), array('POST')) in RouteCollection.php line 176 at RouteCollection->match(object(Request)) in Router.php line 533 ...

I want to test a POST route method via the browser.

回答1:

Change it

Route::post(

to

Route::get(

and try again.

Explanation: Route::post() is used when we want to post the form with some data in it.



回答2:

Route::get('foo/bar', function () {
    return 'Hello World';
});

use this as you are not posting any data thus laravel is looking for data which is not present thus the error