php Laravel:Route::resource() GET & POST 工作,但 PUT & DELETE 抛出 MethodNotAllowedHttpException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27029025/
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: Route::resource() GET & POST work, but PUT & DELETE throw MethodNotAllowedHttpException
提问by mOrloff
I'm writing a webservice API (in laravel 4.2).
For some reason, the routing to one of my controllers is selectively failing based on HTTP method.
我正在编写一个网络服务 API(在 laravel 4.2 中)。
出于某种原因,到我的一个控制器的路由基于 HTTP 方法有选择地失败。
My routes.php looks like:
我的 routes.php 看起来像:
Route::group(array('prefix' => 'v2'),
function()
{
Route::resource('foo', 'FooController',
[ 'except' => ['edit', 'create'] ]
);
Route::resource('foo.bar', 'FooBarController',
[ 'except' => ['show', 'edit', 'create'] ]
);
}
);
So, when I try any of GET / POST / PUT / PATCH / DELETE methods for theproject.dev/v2/foo
or project.dev/v2/foo/1234
urls, everything works perfectly.
因此,当我为project.dev/v2/foo
或project.dev/v2/foo/1234
url尝试任何 GET / POST / PUT / PATCH / DELETE 方法时,一切正常。
But, for some reason, only GET and POST work for project.dev/v2/foo/1234/bar
. The other methods just throw a 405 (MethodNotAllowedHttpException).
(fyi, I am issuing requests via the Advanced Rest ClientChrome extension.)
但是,出于某种原因,只有 GET 和 POST 对project.dev/v2/foo/1234/bar
. 其他方法只是抛出 405 (MethodNotAllowedHttpException)。
(仅供参考,我通过Advanced Rest ClientChrome 扩展发出请求。)
What's going on?
What am I missing?
这是怎么回事?
我错过了什么?
回答by mOrloff
Solved!
The answer can be found by running php artisan routes
.
解决了!
可以通过运行找到答案php artisan routes
。
That showed me that DELETE and PUT/PATCH expect (require) a bar ID.
I happened to be neglecting that because there can only be one of this particular type of "bar". The easy fix it to simply add it to my URL's regardless, like project.dev/v2/foo/1234/bar/5678
.
这向我展示了 DELETE 和 PUT/PATCH 期望(需要)一个栏 ID。
我碰巧忽略了这一点,因为这种特殊类型的“酒吧”只能有一种。简单地修复它,只需将其添加到我的 URL 中,例如project.dev/v2/foo/1234/bar/5678
.
回答by stackMonk
For the ones who are using Laravel versions > 4.2 use this :
对于使用 Laravel 版本 > 4.2 的用户,请使用:
php artisan route:list
This will give the list of routes set in your application. Check if routes for PUT and DELETE are allowed in your routes or not. 405 error is mostly because there is no route for these methods.
这将给出在您的应用程序中设置的路由列表。检查您的路线中是否允许 PUT 和 DELETE 的路线。405错误主要是因为这些方法没有路由。
回答by guizo
I don't know about older Laravel versions. But I use Laravel since 5.2 and it is necessary to include a hidden method input when using put, patch or delete.
我不知道旧的 Laravel 版本。但是我从 5.2 开始使用 Laravel,并且在使用 put、patch 或 delete 时需要包含一个隐藏的方法输入。
Ex:
前任:
<input type="hidden" name="_method" value="PUT">
Check https://laravel.com/docs/5.6/routing#form-method-spoofing
回答by nihar nayak
Just add a hidden input field to your form
只需在表单中添加一个隐藏的输入字段
<input type="hidden" name="_method" value="PUT">
And keep form method as post
并将表单方法保留为帖子
<form method="post" action="{{action('')}}">
回答by Ailton Gomes Oliveira
If you want to use the method PUT in submit form you mast to see this link https://laravel.com/docs/5.6/routing#form-method-spoofing
如果您想在提交表单中使用 PUT 方法,请查看此链接 https://laravel.com/docs/5.6/routing#form-method-spoofing
But if you use ajax in your project you mast to do anything like this:
但是,如果您在项目中使用 ajax,则必须执行以下操作:
<form>
@method('PUT')
// your_element
on your script add:
在您的脚本中添加:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url: {{ route('your_route', ':id') }},
type: 'POST',
data: data,
dataType: 'json',
cache: false,
}).done(function(data,status){
// anything
}).fail(function(){
// anything
});
});