Cannot access POST route in Laravel via browser

亡梦爱人 提交于 2019-12-25 05:24:10

问题


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



来源:https://stackoverflow.com/questions/43845728/cannot-access-post-route-in-laravel-via-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!