Laravel:表单模型绑定和资源控制器错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17444826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel: Form model binding and resource controller error
提问by MatterGoal
I'm building a really simple CRUD in laravel just to learn something about this framework. It works all like a charm but I can't make the update function of a controller work properly.
我正在 laravel 中构建一个非常简单的 CRUD,只是为了了解这个框架。它就像一个魅力,但我无法使控制器的更新功能正常工作。
Here my situation:
这是我的情况:
1)I build a resource controller using artisan command.
1)我使用 artisan 命令构建了一个资源控制器。
2)I build a form view using blade and I Open the form with this code:
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
It works great and every field are filled with the right data. The generate url of the form's actionis:
它工作得很好,每个字段都填充了正确的数据。表单动作的生成网址是:
http://localhost/mysite/task/2
The problem is that when I submit this form I get this error:
问题是,当我提交此表单时,出现此错误:
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
Someone can understand why? Can I help you with more information?
有人能理解为什么吗?我可以帮您提供更多信息吗?
采纳答案by edenstrom
You need 'method' => 'put'.
您需要“方法”=>“放置”。
{{ Form::model($task, array('route' => array('task.update', $task->id), 'files' => true, 'method' => 'PUT')) }}
{{ Form::model($task, array('route' => array('task.update', $task->id), 'files' => true, 'method' => 'PUT')) }}
As you can see here.
正如你在这里看到的。
http://laravel.com/docs/controllers#resource-controllers
http://laravel.com/docs/controllers#resource-controllers
Verb: PUT/PATCH
Path: /resource/{id}
action: update
route: resource.update
EDIT: To trigger the update()-action you must send a PUT or PATCH-request to the route resource.update
, in your case task.update
.
编辑:要触发 update()-action,您必须向 route 发送 PUT 或 PATCH-request resource.update
,在您的情况下task.update
。
回答by Abel Jojo
The only error in your code is that you did not passed PUTor PATCH as HTTP method for your form submission to server.
您的代码中唯一的错误是您没有将 PUTor PATCH 作为 HTTP 方法传递给您的表单提交到服务器。
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException is triggered on such states.
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException 在这种状态下被触发。
a demo model form will be as
演示模型表单将作为
Form::model($name_model, array('action' => array('Controller_name@method', $argument), 'files' => true, 'method' => 'PUT'))
or with route name as
或路线名称为
Form::model($name_model, array('route' => array('route.name', $argument), 'files' => true, 'method' => 'PUT'))
回答by kJamesy
You have a problem with the form action. Assuming you have a route like this:
您的表单操作有问题。假设你有这样的路线:
Route::post('task/update/{id}, function()
{
});
Then, your model-bound form should be:
然后,您的模型绑定表单应该是:
{{ Form::model($task, array('url'=>array('task/update',$task->id),'files'=>true)) }}