Laravel 4.2关于删除的MethodNotAllowedHttpException

时间:2014-09-12 17:18:34

标签: php laravel laravel-4 laravel-routing

我是Laravel的新手,所以有一个项目,一个简单的CRUD,但当我尝试删除数据时删除方法不起作用,我不知道为什么。这是错误:

错误:

throw new MethodNotAllowedHttpException($others);

控制器:

public function destroy($id)
{
    $project = Project::find($id);
    if($project->user_id==Auth::id()) {
        $project->delete();
        return Redirect::to('/');
    } else {
        Session::flash('message', 'You can't delete this!');
        return Redirect::to('/');
    }
}

查看:

{{Form::open(array('url' => 'project/destroy/'.$p->id, 'method' => 'DELETE'))}}
    {{Form::submit("Delete", array('class' => 't2tButton text-center'))}}
{{Form::close()}}

路线:

Route::post('/project/destroy/{id}', "ProjectController@destroy");

2 个答案:

答案 0 :(得分:5)

您的路线设置为POST,但不是DELETE

尝试将此添加到您的路线:

Route::delete('/project/destroy/{id}', "ProjectController@destroy");

或者您可以将方法更改为POST并按原样保留路线,但为了保持RESTful,最好更改为DELETE

答案 1 :(得分:0)

我刚刚想出答案,错误就在路线的这一行:

Route::delete('/project/destroy/{id}', "ProjectController@destroy");

路线方法必须为DELETE