Laravel 如何处理来自浏览器的 PUT 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14756994/
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
How does Laravel handle PUT requests from browsers?
提问by Darrrrrren
I know browsers only support POST
and GET
requests, and Laravel supports PUT
requests using the following code:
我知道浏览器只支持POST
和GET
请求,而 Laravel 支持PUT
使用以下代码的请求:
<?= Form::open('/path/', 'PUT'); ?>
... form stuff ...
<?= Form::close(); ?>
This produces the following HTML
这将产生以下 HTML
<form method="POST" action="http://example.com/home/" accept-charset="UTF-8">
<input type="hidden" name="_method" value="PUT" />
... form stuff ...
</form>
How does the framework handle this? Does it capture the POST
request beforedeciding which route to send the request off to? Does it use ajax to send an actual PUT
to the framework?
框架如何处理这个?它是否在决定将请求发送到哪个路由之前捕获POST
请求?它是否使用 ajax 向框架发送实际数据?PUT
回答by Laurence
It inserts a hidden field, and that field mentions it is a PUT or DELETE request
它插入一个隐藏字段,该字段提到它是一个 PUT 或 DELETE 请求
见这里:
echo Form::open('user/profile', 'PUT');
results in:
结果是:
<input type="hidden" name="_method" value="PUT">
Then it looks for _method when routing in the request.php
core file (look for 'spoofing' in the code) - and if it detects it - will use that value to route to the correct restful controller.
然后它在request.php
核心文件中路由时查找 _method (在代码中查找“欺骗”) - 如果检测到它 - 将使用该值路由到正确的 restful 控制器。
It is still using "POST" to achieve this. There is no ajax used.
它仍然使用“POST”来实现这一点。没有使用ajax。
回答by William Cahill-Manley
Laravel uses the symfony Http Foundation which checks for this _method variable and changes the request to either PUT or DELETE based on its contents. Yes, this happens before routing takes place.
Laravel 使用 symfony Http Foundation,它检查这个 _method 变量并根据其内容将请求更改为 PUT 或 DELETE。是的,这发生在路由发生之前。
回答by Kieran Fahy
You can also use an array within your form open like so:
您还可以在打开的表单中使用数组,如下所示:
{{ Form::open( array('route' => array('equipment.update', $item->id ),
'role' => 'form',
'method' => 'put')) }}
Simply change the method to what you want.
只需将方法更改为您想要的。
回答by Jeremy Harris
While a late answer, I feel it is important to add this for anyone else who finds this and can't get their API to work.
虽然是一个迟到的答案,但我认为为发现此问题且无法使他们的 API 工作的任何其他人添加此内容很重要。
When using Laravel's resource routes like this:
像这样使用 Laravel 的资源路由时:
Route::resource('myRoute','MyController');
It will expect a PUT
in order to call the update()
method. For this to work normally (outside of a form submission), you need to make sure you pass the ContentTypeas x-www-form-urlencoded. This is default for forms, but making requests with cURL or using a tool like Postmanwill not work unless you set this.
它将期望 aPUT
以调用该update()
方法。为了使其正常工作(在表单提交之外),您需要确保将ContentType作为x-www-form-urlencoded传递。这是forms 的默认设置,但除非您设置此项,否则使用 cURL 或使用类似Postman 之类的工具发出请求将不起作用。
回答by Muhammad Adeel
PUT usually refers to update request.
PUT 通常是指更新请求。
When you open a form inside laravel blade template using,
当你在 laravel 刀片模板中打开一个表单时,
{{ Form::open('/path/', 'PUT') }}
It would create a hidden field inside the form as follows,
它将在表单内创建一个隐藏字段,如下所示,
<input type="hidden" name="_method" value="PUT" />
In order for you to process the PUT request inside your controller, you would need to create a method with a put prefix,
为了让您在控制器内处理 PUT 请求,您需要创建一个带有 put 前缀的方法,
for example, putMethodName()
so if you specify,
所以如果你指定,
{{ Form::open('controller/methodName/', 'PUT') }}
inside Form:open. Then you would need to create a controller method as follows,
内部形式:开放。然后你需要创建一个控制器方法,如下所示,
class Controller extends BaseController {
public function putMethodName()
{
// put - usual update code logic goes here
}
}
回答by mhndev
in reality there is no PUT request ! its POST request ! when you tell laravel ; hey laravel this is a PUT request ! it means you want to update some resource ! {{Form::open('user/profile', 'PUT')}} the above line means its PUT request (just it means updating a resource as I mentioned before there is no PUT request ) that line of code create a hidden filed in form that specify the type of request and when laravel router get the request it search for PUT handler of the URI for example sth like : Route::put('the/url/you/mention/here/' , 'someController@someAction');
实际上没有 PUT 请求!它的 POST 请求!当你告诉 laravel 时;嘿 laravel 这是一个 PUT 请求!这意味着您要更新某些资源!{{Form::open('user/profile', 'PUT')}} 上面这行表示它的 PUT 请求(只是它意味着更新资源,正如我之前提到的,没有 PUT 请求)那行代码创建了一个隐藏的以指定请求类型的形式提交,当 Laravel 路由器收到请求时,它会搜索 URI 的 PUT 处理程序,例如:Route::put('the/url/you/mention/here/' , 'someController@ someAction');