在Laravel 5.4的路由中使用DELETE方法 [英] use DELETE method in route with Laravel 5.4

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

问题描述

我正在研究Laravel(v 5.4)项目,并且我进行了CRUD管理类别.目前,我可以创建一个新类别,也可以删除.

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.

我创建了带有刀片的视图以删除类别:

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>

在路由文件web.php中,我写道:

And in the routing file web.php, i wrote :

Route::delete('/categories/{id}', CategoryController@destroy);

我有一个带有Category()方法的Controller CategoryController,该方法会删除类别并重定向到类别列表.但是,当我单击要删除的按钮时,出现错误,说明此路由未定义.如果我将Route::delete替换为Route::get,它将起作用.我认为该url是使用GET调用的,但我会保留它以进行其他操作.

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.

我尝试将链接替换为表单,并将"DELETE"作为"method"属性的值,但是它不起作用.

I tried to replace the link with a form and "DELETE" as the value of "method" attribute but it didn't work.

如何使用DELETE方法调用url以使用Route::delete捕获它?

How can i call url with DELETE method to catch it with Route::delete ?

谢谢.

推荐答案

如果单击某个URL,它将始终是GET方法.

If you click on an url it will always be a GET method.

由于您希望将其定义为DELETE,因此应将其重新制成邮寄表格并添加

Since you wish to define it as DELETE, you should remake it into a post form and add

<input type="hidden" name="_method" value="delete" />

在其中.像替换一样:

<a href="{{ url('/categories', ['id' => $category->id]) }}">
    <button class="btn btn-default">Delete</button>
</a>

具有:

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

PUT请求也一样.

由于 Laravel 5.1 method_field:

Since Laravel 5.1 method_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>

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>

这篇关于在Laravel 5.4的路由中使用DELETE方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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