【问题标题】:How to handle DELETE request in Laravel如何在 Laravel 中处理 DELETE 请求
【发布时间】:2015-11-02 20:02:05
【问题描述】:

单击删除项目链接时出现此错误:

RouteCollection.php 第 219 行中的 MethodNotAllowedHttpException:

在 RouteCollection.php 第 219 行

在 RouteCollection.php 第 206 行中的 RouteCollection->methodNotAllowed(array('DELETE'))

这是链接:

<a href="{{ url('cats/'.$cat->id.'/delete') }}">
    <span class="glyphicon glyphicon-trash"></span>
    Delete
</a>

这就是我在routes.php 尝试处理它的方式:

Route::delete('cats/{cat}/delete', function(Furbook\Cat $cat){
    $cat->delete();
    return redirect('cats')->withSuccess('Cat has been deleted');
});

完整的routes.php:

<?php 
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

Route::get('/', function () {
        return redirect('cats');
});

Route::get('cats', function () {
    $cats = Furbook\Cat::All();
        return view('cats.index')->with('cats',$cats);
});

Route::get('cats/create', function(){
    return view('cats.create');
});

Route::post('cats', function(){
    $cat = Furbook\Cat::create(Input::all());
    return redirect('cats/'.$cat->id)->withSuccess('Cat has been created');
});

Route::get('cats/{id}', function ($id) {
    $cat = Furbook\Cat::findOrNew($id);
    return view('cats.show')->with('cat',$cat);
});

Route::get('cats/{cat}', function(Furbook\Cat $cat){
    return view('cats.show')->with('cat',$cat);
});

Route::delete('cats/{cat}/delete', function(Furbook\Cat $cat){
    $cat->delete();
    return redirect('cats')->withSuccess('Cat has been deleted');
});

Route::get('about', function () {
        return view('about')->with('number_of_cats',9000);
});

Route::get('cats/breeds/{name}', function ($name) {
    $breed = Furbook\Breed::with('cats')
        ->whereName($name)
        ->first();
        $cats = null;
        if(isset($breed))
                $cats=$breed->cats;
    return view('cats.index')
        ->with('breed',$breed)
        ->with('cats',$cats);
});

└─(21:18:40)──> php artisan route:list

+--------+----------+--------------------+------+---------+------------+
| Domain | Method   | URI                | Name | Action  | Middleware |
+--------+----------+--------------------+------+---------+------------+
|        | GET|HEAD | /                  |      | Closure |            |
|        | GET|HEAD | about              |      | Closure |            |
|        | POST     | cats               |      | Closure |            |
|        | GET|HEAD | cats               |      | Closure |            |
|        | GET|HEAD | cats/breeds/{name} |      | Closure |            |
|        | GET|HEAD | cats/create        |      | Closure |            |
|        | GET|HEAD | cats/{cat}         |      | Closure |            |
|        | DELETE   | cats/{cat}/delete  |      | Closure |            |
|        | GET|HEAD | cats/{id}          |      | Closure |            |
+--------+----------+--------------------+------+---------+------------+

【问题讨论】:

  • 请添加完整的路线列表
  • 已添加,请检查修改
  • 您是否也可以通过在命令行中输入“php artisan routes”来检查您的路线
  • 在这里,顺便说一句,我一直在阅读的内容似乎 Thomas 是对的。所以,Laravel 5 essentials book 中有一个错误:-/
  • 是的,Thomas 是对的,我只是在看到您的路线列表后才来申请表单。 @Thomas,谢谢

标签: php laravel


【解决方案1】:

链接向服务器发送GET 请求,但您的路由期待DELETE 请求。你需要做这样的事情来发送DELETE 请求。

<form method="POST" action="{{ url('cats/'.$cat->id.'/delete') }}">
    {{ csrf_field() }}
    <input type="hidden" name="_method" value="DELETE">
    <span class="glyphicon glyphicon-trash"></span>
    <button type="submit">Delete</button>
</form>

在 HTML 中,只有 GETPOST 请求。没有PUTPATCHDELETE 方法。 Laravel(在某种意义上)通过添加一个名为 _method 的隐藏输入字段来“模拟”这些方法,该字段指定这些方法。

由于 HTML 表单仅支持 POST 和 GET,因此 PUT 和 DELETE 方法将通过自动添加 _method 隐藏字段到您的表单来进行欺骗。

来源:http://laravel.com/docs/4.2/html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-31
    • 2019-04-18
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 2021-08-17
    • 2021-07-26
    • 1970-01-01
    相关资源
    最近更新 更多