Laravel 5 MethodNotAllowedHttpException PUT

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35431617/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:14:48  来源:igfitidea点击:

Laravel 5 MethodNotAllowedHttpException PUT

phplaravelmethodslaravel-5

提问by simon volk

I am trying to update a user, but when I hit the submit button, Laravel throws the below error:

我正在尝试更新用户,但是当我点击提交按钮时,Laravel 抛出以下错误:

"RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'POST')) in RouteCollection.php line 206".

“RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'POST')) 在 RouteCollection.php 第 206 行”。

I think that the PUTmethod is not allowed, but I do not understand the reason. The request never reaches UserController@update.

我认为该PUT方法是不允许的,但我不明白原因。请求永远不会到达UserController@update

I have configured a resource route like this:

我已经配置了这样的资源路由:

Route::resource('backend/users', 'Backend\UsersController');

The output of php artisan route:listis:

php artisan 的输出route:list是:

output of php artisan route:list

php artisan route 的输出:列表

回答by allen

I solved the problem like this: it must be the form's post action error;

我是这样解决的:一定是表单的post action错误;

<form method="POST" action="10.241.229.1/backend/users/{{$user->id}}"; accept-charset="UTF-8">

<form method="POST" action="10.241.229.1/backend/users/{{$user->id}}"; accept-charset="UTF-8">

add the id you want update to the action.

将您要更新的 ID 添加到操作中。

enter image description here

在此处输入图片说明

回答by Imtiaz Pabel

use put method like this within form,for more https://laravel.com/docs/5.2/routing#form-method-spoofing

在表单中使用这样的 put 方法,了解更多https://laravel.com/docs/5.2/routing#form-method-spoofing

{{ method_field('PUT') }}

回答by Giorgio Ghiatis

Coming a bit late on this question.

这个问题来得有点晚。

In my experience this kind of error comes for two reasons:

根据我的经验,出现这种错误有两个原因:

  1. as Laravel docssay, HTML forms do not support PUT, PATCHor DELETEactions. So, when defining PUT, PATCHor DELETEroutes that are called from an HTML form, you will need to add a hidden _methodfield to the form.

  2. if you are making the request from a HTML form, and you have the VerifyCsrfTokenmiddleware enable, than you will need to add a hidden _tokenfield to the form with {{ csrf_token() }}as value.

  1. 正如Laravel 文档所说,HTML 表单不支持PUT,PATCHDELETE动作。因此,当定义PUT,PATCHDELETE从 HTML 表单调用的路由时,您需要向表单添加一个隐藏_method字段。

  2. 如果您从 HTML 表单发出请求,并且VerifyCsrfToken启用了中间件,则您需要_token使用{{ csrf_token() }}as 值向表单添加一个隐藏字段。