What is the value of using PUT/DELETE with Laravel

2019-03-28 11:01发布

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

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.

Here's the core of what I understand about these interactions with Laravel:

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 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?

Essentially, if PUT and DELETE in Laravel are functionally identical to POST, aside from REST semantics and parallel routing, when and why should I use them over POST?

标签: php laravel-4
1条回答
贼婆χ
2楼-- · 2019-03-28 11:49

You use PUT method when you want to Update a record, And you use DELETE method when you want to Delete a record.

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.

查看更多
登录 后发表回答