如何在Laravel中处理DELETE请求

viv*_*ino 1 php laravel

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

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>
Run Code Online (Sandbox Code Playgroud)

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

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

完成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);
});
Run Code Online (Sandbox Code Playgroud)

└─(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 |            |
+--------+----------+--------------------+------+---------+------------+
Run Code Online (Sandbox Code Playgroud)

Tho*_*Kim 5

链接将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>
Run Code Online (Sandbox Code Playgroud)

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

由于HTML表单仅支持POST和GET,因此通过自动向表单添加_method隐藏字段来欺骗PUT和DELETE方法.

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