使用Route :: resource()在laravel中删除 [英] delete in laravel with get using Route::resource()
问题描述
我正在尝试使用GET而不是DELETE在laravel中删除,因为我的共享服务器不支持DELETE动词.
I'm trying to delete in laravel using GET instead of DELETE because my shared server doesn't support DELETE verb.
因此,我使用了 Jeffrey Way的方法
东西在我的routes.php中,我使用
Thing is in my routes.php, I use
Route::resource('users', 'UserController');
例如.
因此,当我对资源使用GET而不是DELETE时,系统认为我将使用show方法而不是destroy方法.
So, When I use GET instead of DELETE with resource, system think I will use show method, instead of destroy method.
我看到的唯一方法是不使用资源方法进行路由,并详细说明我的所有路由,但我不喜欢它,阅读起来有点繁琐.
The only way I see to do that is not using resource method to route, and detail all my routes, but I don't like it, it is kind of heavy to read.
有没有办法继续使用resource()并有一条定制的销毁路线?
Is it any way to keep using resource() and have a customized route to destroy?
Tx!
推荐答案
HTML表单实际上不支持PUT
,PATCH
或DELETE
操作;它们仅支持GET
或POST
请求.
HTML forms don't actually support PUT
, PATCH
, or DELETE
actions; they only support GET
or POST
requests.
相反,Laravel欺骗了该方法,使您可以使用隐藏的_method
字段使用这些字段,您可以在文档.
Instead, Laravel spoofs the method to allow you to use these using a hidden _method
field, which you can read up on in the docs.
使用Route::resource
会将DELETE
方法自动路由到控制器中的destroy
函数.
Using Route::resource
will automatically route DELETE
methods to your destroy
function in the controller.
如果您使用的是表单助手,则可以在Form::open()
中使用以下内容指定删除方法:
If you are using Form helpers, then you can use the following in your Form::open()
to specify the delete method:
{!! Form::open(['method' => 'DELETE']) !!}
如果不是,则可以像{{ method_field('DELETE') }}
这样将其包含在HTML表单中.
If you aren't then you can simply include it like so {{ method_field('DELETE') }}
inside your HTML form.
如果您不欺骗它并使用GET
请求,则Route::resource
会将其与控制器中的show
函数关联.
If you don't spoof it and use a GET
request then the Route::resource
will associate this with the show
function in your controller.
使用按钮执行此操作
{!! Form::open(['method' => 'DELETE']) !!}
{!! Form::submit('Delete') !!}
{!! Form::close() !!}
这篇关于使用Route :: resource()在laravel中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!