【问题标题】:Laravel 5.2 MethodNotAllowedHttpException with Patch on route::resourceLaravel 5.2 MethodNotAllowedHttpException with Patch on route::resource
【发布时间】:2016-06-23 01:46:30
【问题描述】:

我想更新一个帖子。创建和删除帖子效果很好,但是每当我尝试使用 PATCH 表单更新它时,它都会失败并给出MethodNotAllowedHttpException

我的路线.php:

...
Route::resource('posts', 'PostsController');
... 

这给了我以下可能的路线列表(粘贴在 css 中以保持可读性):

| GET|HEAD  | posts                    | posts.index          | App\Http\Controllers\PostsController@index   
| POST      | posts                    | posts.store          | App\Http\Controllers\PostsController@store    
| GET|HEAD  | posts/create             | posts.create         | App\Http\Controllers\PostsController@create     
| GET|HEAD  | posts/{posts}            | posts.show           | App\Http\Controllers\PostsController@show        
| DELETE    | posts/{posts}            | posts.destroy        | App\Http\Controllers\PostsController@destroy        
| PUT|PATCH | posts/{posts}            | posts.update         | App\Http\Controllers\PostsController@update      
| GET|HEAD  | posts/{posts}/edit       | posts.edit           | App\Http\Controllers\PostsController@edit   

我的edit.blade.php (url= localhost:8000/posts/1/edit):

 {!! Form::model($post, ['method' => 'PATCH', 'action' => ['PostsController@update', $post]]) !!}
        @include('posts/_form', array('submitText' => 'Update'))
   {!! Form::close() !!}

还有我的 PostsController:

public function update(Request $request, Post $post) {
        $post->update($request->all());
         return Redirect::route('posts.index')->with('message_succes', 'Post updated');
}

无论我尝试什么,它都失败了

MethodNotAllowedHttpException RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'POST')) RouteCollection.php 第 206 行

查看表单的 html 源代码,PATCH 和令牌已正确插入。 当在表单中将 PATCH 更改为 post 时,它将使用 store 功能并创建一个新帖子。我需要做什么来更新帖子?

【问题讨论】:

  • 您应该只传递一个带有表单操作的 id。你使用了$post。由于您没有共享edit() 方法,我们不知道$post 变量中的返回值。如果 $post 是一个集合,请使用 ['PostController@update',$post->id]。如果不行,请分享edit()方法的代码。
  • @smartrahat 是的,你是对的。已经发现我确实发布了完整的对象。它现在正在工作。

标签: forms routes crud laravel-5.2


【解决方案1】:

原来我发布的是完整的对象,而不仅仅是 ID。 我不得不改变:

$post$post->id 表单和 PostController 更新为:

  public function update(Request $request, $post_id) {
        $post = Post::findOrFail($post_id)
        $post->update($request->all());
         return Redirect::route('posts.index')->with('message_succes', 'Post updated');
}

【讨论】:

    【解决方案2】:

    尝试将 PATCH 方法更改为这样的形式:

    {!! Form::model($post, ['method' => 'PUT', 'action' => ['PostsController@update', $post]]) !!}
        @include('posts/_form', array('submitText' => 'Update'))
    {!! Form::close() !!}
    

    【讨论】:

    • 感谢您的回复。它给了我同样的错误。还有一点要说的是,这些字段没有填充条目。
    【解决方案3】:

    对于那些忘记为编辑表单指定操作/路线的人,即:

    {!! Form::model($post, ['method' => 'PATCH']) !!}
        ...
        Form controls
        ...
    {!! Form::close() !!}
    

    如果您没有在edit 页面上明确指定update 表单操作,Form::model() 将使用当前路由,例如<site>/posts/<id>/edit。所以,不要忘记真正的update 操作位置,无论是通过路由:

    {!! Form::model($post, ['method' => 'PATCH', 'route' => ['posts.update', $post]]) !!}
        ...
        Form controls
        ...
    {!! Form::close() !!}
    

    ...我个人更喜欢,因为它更通用,或者通过行动:

    {!! Form::model($post, ['method' => 'PATCH', 'action' => ['PostsController@update', $post->id]]) !!}
        ...
        Form controls
        ...
    {!! Form::close() !!}
    

    ...有点过于冗长和具体。

    【讨论】:

      【解决方案4】:

      很好,你是对的!但是您将 $post->id 注释为反向,然后控制器将使用整个对象而不是单个对象。现在在这里工作。谢谢!

      【讨论】:

        猜你喜欢
        • 2015-11-18
        • 2016-08-31
        • 2018-01-13
        • 1970-01-01
        • 2016-12-22
        • 2014-06-23
        • 1970-01-01
        • 1970-01-01
        • 2014-06-23
        相关资源
        最近更新 更多