【问题标题】:Laravel: Form model binding and resource controller errorLaravel:表单模型绑定和资源控制器错误
【发布时间】:2013-07-03 09:51:36
【问题描述】:

我正在 laravel 中构建一个非常简单的 CRUD,只是为了了解这个框架。 它就像一个魅力,但我不能让控制器的更新功能正常工作。

这是我的情况:

1) 我使用 artisan 命令构建了一个资源控制器。

2) 我使用刀片构建表单视图并使用以下代码打开表单:

<!-- Form -->
@if($mode=="edit")
    {{ Form::model($task, array('route'=>array('task.update',$task->id),'files'=>true)) }}
@else
    {{ Form::open(array('route'=>'task.store','files'=>true)) }}
@endif

效果很好,每个字段都填充了正确的数据。 表单动作的生成url为:

http://localhost/mysite/task/2

问题是,当我提交此表单时,我收到此错误:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

有人能理解为什么吗?我可以帮助您了解更多信息吗?

【问题讨论】:

    标签: php laravel laravel-4


    【解决方案1】:

    你需要'方法' => '放置'。

    {{ Form::model($task, array('route' =&gt; array('task.update', $task-&gt;id), 'files' =&gt; true, 'method' =&gt; 'PUT')) }}

    如您所见。

    http://laravel.com/docs/controllers#resource-controllers

    Verb:     PUT/PATCH
    Path:     /resource/{id}
    action:   update
    route:    resource.update
    

    编辑:要触发 update() 操作,您必须向路由 resource.update 发送 PUT 或 PATCH 请求,在您的情况下为 task.update

    【讨论】:

      【解决方案2】:

      您的表单操作有问题。假设您有这样的路线:

      Route::post('task/update/{id}, function()
      {
      
      });
      

      那么,你的模型绑定表单应该是:

      {{ Form::model($task, array('url'=>array('task/update',$task->id),'files'=>true)) }}
      

      【讨论】:

        【解决方案3】:

        您的代码中唯一的错误是您没有将 PUTor PATCH 作为 HTTP 方法传递给您的表单提交到服务器。

        Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException 在这些状态下被触发。

        演示模型表单将如下所示

         Form::model($name_model, array('action' => array('Controller_name@method', $argument), 'files' => true, 'method' => 'PUT'))
        

        或路由名称为

        Form::model($name_model, array('route' => array('route.name', $argument), 'files' => true, 'method' => 'PUT'))
        

        【讨论】:

          猜你喜欢
          • 2018-01-16
          • 2021-02-06
          • 1970-01-01
          • 2016-12-30
          • 1970-01-01
          • 2020-07-17
          • 2013-05-28
          • 2014-05-29
          • 1970-01-01
          相关资源
          最近更新 更多