【问题标题】:Laravel 4.2 MethodNotAllowedHttpException on deleteLaravel 4.2 MethodNotAllowedHttpException 删除
【发布时间】:2014-11-06 22:38:43
【问题描述】:

我是 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");

【问题讨论】:

  • 你确定 PHP 引擎不会抛出语法错误吗?这个:'You can't delete this!' - 你必须用双引号括起来这个刺""
  • 好点@bad_boy,或者像这样can\'t逃避can't中的'can\'t

标签: php laravel laravel-4 laravel-routing


【解决方案1】:

您为POST 设置了路线,但没有为DELETE 设置路线。

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

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

或者您可以将您的方法更改为 POST 并保持您的路线不变,但要使其保持 RESTful,最好更改为 DELETE

【讨论】:

  • 没错,就是这个问题哈哈哈我刚贴出来,谢谢=)。
  • @bad_boy 他至少要等 15 分钟...但是如果有帮助,请在该时间过去后接受...谢谢
【解决方案2】:

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

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

Route 方法需要是DELETE

【讨论】:

    猜你喜欢
    • 2014-07-30
    • 2015-02-02
    • 2017-02-17
    • 2018-11-25
    • 2018-05-28
    • 2015-10-29
    • 2015-01-14
    • 2016-01-07
    • 2018-09-15
    相关资源
    最近更新 更多