Laravel 5.5 - 使用 POST 方法时的 MethodNotAllowedHttpException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46259927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.5 - MethodNotAllowedHttpException when using POST method
提问by User3100
I created filter to get data from database. When I use GET method it works but with POST method I get error:
我创建了过滤器来从数据库中获取数据。当我使用 GET 方法时,它可以工作,但使用 POST 方法时出现错误:
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
I spent hours looking for solution but seems like I am missing something here.
我花了几个小时寻找解决方案,但似乎我在这里遗漏了一些东西。
For test I tried to get results directly in routes but it's still same problem.
为了测试,我试图直接在路由中获得结果,但它仍然是同样的问题。
This works.
这有效。
<?php
use App\test;
Route::get('/', function () {
$test = test::all();
return $test;
});
This doesn't work.
这不起作用。
<?php
use App\test;
Route::post('/', function () {
$test = test::all();
return $test;
});
回答by Michel
This
这个
use App\test;
Route::post('/', function () {
$test = test::all();
return $test;
});
Should always be an instance of Request
so you can't access it directly in the browser like GET
you have to post some form data to it. So it's better you rename it to something, maybe like:
应该总是一个实例,Request
这样你就不能直接在浏览器中访问它,就像GET
你必须向它发布一些表单数据一样。因此,最好将其重命名为某个名称,例如:
use App\test;
Route::post('/test', function (Request $request) {
$test = test::all();
return $test;
});
This $request
hold your form data
这$request
保存您的表单数据
回答by patricus
Route::get()
and Route::post()
are defining the route handlers for the specified http methods.
Route::get()
并且Route::post()
正在为指定的 http 方法定义路由处理程序。
If you only define this route:
如果您只定义此路由:
Route::post('/', function () {
$test = test::all();
return $test;
});
Then you must make sure all calls to that url use the POST method. If you make a request to that url with the GET method, you'll get the MethodNotAllowedHttpException
exception, since you only defined the POST method handler.
然后,您必须确保对该 url 的所有调用都使用 POST 方法。如果您使用 GET 方法向该 url 发出请求,您将收到MethodNotAllowedHttpException
异常,因为您只定义了 POST 方法处理程序。
回答by Monir Khan
It's look like you may forgot about {{ csrf_field() }}. Add this to your form.
看起来您可能忘记了 {{ csrf_field() }}。将此添加到您的表单中。
If you are posting data without the csrf, in laravel 5.5 you will see this error.
如果您在没有 csrf 的情况下发布数据,则在 laravel 5.5 中您将看到此错误。
回答by Sanju Kaniyamattam
Please note that if you are writing the route for post, when testing it make sure that your are posting the data to the page otherwise it will through an error.
请注意,如果您正在编写用于发布的路由,则在测试时确保您将数据发布到页面,否则会出错。
Post routes cannot test simply by calling the url, the method should be post. If you check the route URI using postman it will work.
Post 路由不能简单地通过调用 url 来测试,方法应该是 post。如果您使用邮递员检查路由 URI,它将起作用。