Laravel:表单模型绑定和资源控制器错误

Mat*_*oal 9 php laravel laravel-4

我正在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
Run Code Online (Sandbox Code Playgroud)

它工作得很好,每个领域都充满了正确的数据.表单动作的生成URL 是:

http://localhost/mysite/task/2
Run Code Online (Sandbox Code Playgroud)

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

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
Run Code Online (Sandbox Code Playgroud)

有人能理解为什么吗?我可以帮你提供更多信息吗?

小智 9

你需要'method'=>'put'.

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

正如你在这里看到的那样.

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

Verb:     PUT/PATCH
Path:     /resource/{id}
action:   update
route:    resource.update
Run Code Online (Sandbox Code Playgroud)

编辑:要触发更新() -行动则必须发送PUT或者PATCH请求的路径resource.update,你的情况task.update.