php 在 Laravel 5.4 的路由中使用 DELETE 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44113969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
use DELETE method in route with Laravel 5.4
提问by Needlle
I'm working on a Laravel (v 5.4) project and i did the CRUD to manage categories. Currently, i can create a new category and i would be able to delete.
我正在开发一个 Laravel (v 5.4) 项目,我使用 CRUD 来管理类别。目前,我可以创建一个新类别,我可以删除。
I created the view (with blade) to delete the categories :
我创建了视图(带刀片)来删除类别:
<table class="table">
<thead>
<th>Name</th>
<th>Action</th>
</thead>
<tbody>
@foreach ($categories as $category)
<tr>
<td>$category->name</td>
<td>
<a href="{{ url('/categories', ['id' => $category->id]) }}">
<button class="btn btn-default">
Delete
</button>
</a>
</td>
</tr>
@endforeach
</tbody>
</table>
And in the routing file web.php, i wrote :
在路由文件 web.php 中,我写道:
Route::delete('/categories/{id}', CategoryController@destroy);
I have a controller CategoryController with a method destroy() who delete category and redirect to list of categories. But when i click on the button to delete, i get an error that explain this route is not define. If i replace Route::delete
with Route::get
it works. I think the url is called with GET but i would keep that for an other action.
我有一个带有方法 destroy() 的控制器 CategoryController 删除类别并重定向到类别列表。但是当我点击按钮删除时,我收到一个错误,说明这条路线没有定义。如果我Route::delete
用Route::get
它替换它就可以了。我认为 url 是用 GET 调用的,但我会保留它用于其他操作。
I tried to replace the link with a form and "DELETE" as the value of "method" attribute but it didn't work.
我尝试用表单替换链接,并将“DELETE”替换为“method”属性的值,但没有用。
How can i call url with DELETE method to catch it with Route::delete
?
如何使用 DELETE 方法调用 url 来捕获它Route::delete
?
Thanks in advance.
提前致谢。
回答by Peon
If you click on an url it will always be a GET method.
如果您单击一个 url,它将始终是一个 GET 方法。
Since you wish to define it as DELETE, you should remake it into a post form and add
由于您希望将其定义为 DELETE,您应该将其重新制作为一个帖子表单并添加
<input type="hidden" name="_method" value="delete" />
in it. Like replace:
在里面。喜欢替换:
<a href="{{ url('/categories', ['id' => $category->id]) }}">
<button class="btn btn-default">Delete</button>
</a>
with:
和:
<form action="{{ url('/categories', ['id' => $category->id]) }}" method="post">
<input class="btn btn-default" type="submit" value="Delete" />
<input type="hidden" name="_method" value="delete" />
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
Same goes for PUT request.
PUT 请求也是如此。
Since Laravel 5.1method_field:
从Laravel 5.1method_field 开始:
<form action="{{ url('/categories', ['id' => $category->id]) }}" method="post">
<input class="btn btn-default" type="submit" value="Delete" />
{!! method_field('delete') !!}
{!! csrf_field() !!}
</form>
Since Laravel 5.6just with @ tag:
从Laravel 5.6 开始只使用 @ 标签:
<form action="{{ url('/categories', ['id' => $category->id]) }}" method="post">
<input class="btn btn-default" type="submit" value="Delete" />
@method('delete')
@csrf
</form>
回答by user5510975
For laravel 5.7 please look my example:
对于laravel 5.7,请看我的例子:
<form action="{{route('statuses.destroy',[$order_status->id_order_status])}}" method="POST">
@method('DELETE')
@csrf
<button type="submit">Delete</button>
</form>
回答by Sandeesh
Any method other than GET
and POST
requires you to specify the method type using a hidden form input. That's how laravel detects them. In your case you need to send the delete action using a form. Do this.
除了GET
和POST
要求您使用隐藏表单输入指定方法类型的任何方法。这就是 laravel 检测它们的方式。在您的情况下,您需要使用表单发送删除操作。做这个。
<table class="table">
<thead>
<th>Name</th>
<th>Action</th>
</thead>
<tbody>
@foreach ($categories as $category)
<tr>
<td>$category->name</td>
<td>
<form action="/categories/{{ $category->id }}" method="post">
{{ method_field('delete') }}
<button class="btn btn-default" type="submit">Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>