Laravel 4 从视图中的表单向控制器传递参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17382801/
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 4 passing parameters to Controller from Form in View
提问by user2525589
I want to save data to the database by clicking on a button in a view file.
我想通过单击视图文件中的按钮将数据保存到数据库中。
I would like to achieve this by calling a method in my controller via POST. It works but I have to pass some variables/parameters ( without having inputfields ) to the controller and that doesn't work.
我想通过 POST 在我的控制器中调用一个方法来实现这一点。它有效,但我必须将一些变量/参数(没有 inputfields )传递给控制器,但这是行不通的。
this is my controller:
这是我的控制器:
class CouplesController extends BaseController {
public function postCreate($var1)
{
Couple::create(array(
'name'=>'test',
'id1'=>$var1
));
return Redirect::to('couples')
->with('message','Your couple was created successfully!');
}
}
and this is my view:
这是我的观点:
{{ Form::open(array('action' => 'CouplesController@postCreate', $var1->'hello')) }}
<p>{{ Form::submit('Create') }}</p>
{{ Form::close() }}
probably I'm doing this completely wrong. I just don't know how to do this.
可能我这样做完全错误。我只是不知道该怎么做。
btw it doesn't have to be the POST method.
顺便说一句,它不一定是 POST 方法。
回答by tplaner
You are really close, within your view:
在你看来,你真的很接近:
{{ Form::open(array('action' => 'CouplesController@postCreate', 'hello')) }}
<p>{{ Form::submit('Create') }}</p>
{{ Form::close() }}
This will generate a URL similar to:
这将生成类似于以下内容的 URL:
<form method="POST" action="http://localhost/couples/create/hello" accept-charset="UTF-8">
Then the rest of your code should work without an issue, and $var1
should be set to the value of hello
.
然后您的其余代码应该可以正常工作,并且$var1
应该设置为hello
.
回答by user2525589
I just saw that the error with the missing parameter appears while having this route:
我刚刚看到在这条路线上出现了缺少参数的错误:
Route::post('couples/done', 'CouplesController@postCreate');
when I take this route out it gives me this error:
当我走这条路线时,它给了我这个错误:
Unknown action [CouplesController@postCreate].
未知操作 [CouplesController@postCreate]。
like it doesn't have access to the controller in the view itself.
就像它无权访问视图本身中的控制器一样。
:(
:(