在Laravel中使用PUT/DELETE有什么价值? [英] What is the value of using PUT/DELETE with Laravel?

查看:228
本文介绍了在Laravel中使用PUT/DELETE有什么价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于使用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:

  • You can define a route to accept PUT with Route::put and DELETE with Route::delete.
  • The Input facade uses Illuminate\Http\Request::input typifying interactions and will return json_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.

请注意,在资源丰富的控制器中,PUTDELETE方法都指向相同的url (resource/{resource}),因此,如果不将方法与PUTDELETE区别开来,将是一个问题

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆