使用Route :: resource()在laravel中删除 [英] delete in laravel with get using Route::resource()

查看:306
本文介绍了使用Route :: resource()在laravel中删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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表单实际上不支持PUTPATCHDELETE操作;它们仅支持GETPOST请求.

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

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