简体   繁体   English

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

[英]Laravel: Form model binding and resource controller error

I'm building a really simple CRUD in laravel just to learn something about this framework. 我正在laravel中构建一个非常简单的CRUD,只是为了学习这个框架。 It works all like a charm but I can't make the update function of a controller work properly. 它的工作原理就像一个魅力,但我无法使控制器的更新功能正常工作。

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 action is: 表单动作的生成URL是:

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? 我可以帮你提供更多信息吗?

You need 'method' => 'put'. 你需要'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() - 操作,您必须在您的案例task.update向路由resource.update发送PUT或PATCH请求。

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)) }}

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'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM