在Laravel中使用PUT/DELETE有什么价值? [英] What is the value of using PUT/DELETE with Laravel?
问题描述
对于使用Route::resource
将路线定义为资源,文档指示:
For defining a route as a resource with Route::resource
, the docs indicate:
Verb Path Action Route Name
-------------------------------------------------------------------
GET /resource index resource.index
GET /resource/create create resource.create
POST /resource store resource.store
GET /resource/{resource} show resource.show
GET /resource/{resource}/edit edit resource.edit
PUT/PATCH /resource/{resource} update resource.update
DELETE /resource/{resource} destroy resource.destroy
按照典型的REST CRUD,因此PUT/PATCH用于更新操作,而DELETE用于销毁操作.我认为这是为了定义典型的资源交互,即使手动定义我自己的路线和控制器动作也是如此.
as per typical REST CRUD so PUT/PATCH is used for update actions and DELETE for destroy actions. I would think that is meant to define a typical resource interaction, even when manually defining my own routes and controller actions.
这是我对与Laravel进行交互的了解的核心:
Here's the core of what I understand about these interactions with Laravel:
- 您可以定义一条路径,以使用
Route::put
接受PUT,并使用Route::delete.
接受DELETE
-
Input
外观使用Illuminate\Http\Request::input
表示交互,并将为json返回内容的json_decode
,对于GET请求返回所有GET参数,否则返回所有POST参数.我们必须使用json或根本不使用PUT或DELETE(很好,因为不普遍支持http PUT和DELETE请求). - Laravel自己的表单助手使用POST进行PUT和DELETE请求,包括数据中的
_method
,以便路由可以强制执行动词.
- You can define a route to accept PUT with
Route::put
and DELETE withRoute::delete.
- The
Input
facade usesIlluminate\Http\Request::input
typifying interactions and will returnjson_decode
of content for json, all GET parameters for GET requests or all POST parameters otherwise. We must use json or not actually use PUT or DELETE at all (fine because http PUT and DELETE requests are not universally supported). - Laravel's own form helpers use POST for PUT and DELETE requests, including a
_method
in the data so that the routing can enforce the verbs.
PUT和DELETE是否仅用于创建外部可访问的JSON REST API,还是它们有其他用途?除了路由到具有不同终结点的相同URI,强制在$_POST
或json中存在_method
之外,还有其他好处吗?
Are PUT and DELETE solely there to create externally-accessible json REST APIs or do they serve another purpose? Is there some benefit aside from routing to the same URI with a different endpoint, enforcing the presence of a _method
in $_POST
or the json?
PUT和DELETE应该是幂等的,但这甚至可以在Laravel中实现吗?这是我必须在控制器中执行的操作,还是路由以某种方式强制执行此操作?
PUT and DELETE are supposed to be idempotent, but is this even implemented in Laravel? Is this something that I have to make happen in my controllers or does the routing enforce this somehow?
本质上,如果Laravel中的PUT和DELETE在功能上与POST相同,除了REST语义和并行路由之外,我何时以及为什么应该在POST上使用它们?
推荐答案
要更新记录时,请使用PUT
方法,要删除记录时,请使用DELETE
方法.
You use PUT
method when you want to Update a record, And you use DELETE
method when you want to Delete a record.
请注意,在资源丰富的控制器中,PUT
和DELETE
方法都指向相同的url (resource/{resource})
,因此,如果不将方法与PUT
或DELETE
区别开来,将是一个问题
Note that in the resourceful controller, both PUT
and DELETE
method directed to the same url (resource/{resource})
, so if you don't distinguish the method with PUT
or DELETE
, it will be a problem.
这篇关于在Laravel中使用PUT/DELETE有什么价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!