2015-11-02 82 views
1
DELETE請求

我得到這個錯誤,當我點擊刪除項目鏈接:如何處理Laravel

MethodNotAllowedHttpException在RouteCollection.php行219:

在RouteCollection.php線219

在RouteCollection-> methodNotAllowed(陣列( '刪除'))在RouteCollection.php線206

這是鏈接:

<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分四十秒)─ ─> php artisan route:列表

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

請補充完整路由列表 – davejal

+0

添加,請編輯 – vivoconunxino

+0

是否也能輸入「PHP工匠路線」在您的COMAND行 – davejal

回答

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> 

在HTML中,只有GETPOST請求。沒有PUT,PATCHDELETE方法。 Laravel(某種意義上)通過添加一個名爲_method的隱藏輸入字段來「嘲笑」這些方法,該字段指定了這些方法。

由於HTML表單只支持POST和GET,所以PUT和DELETE方法將被欺騙,方法是自動向表單中添加一個_method隱藏字段。

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

+0

我編輯了一些說明。 :) –