Laravel表单发布到控制器

时间:2016-06-03 10:43:34

标签: php laravel model-view-controller

我是Laravel的新手,我在将数据发布到控制器时遇到了麻烦。我找不到相应的文档。我希望在Laravel中有类似的东西,我在C#MVC中做的事情。

<form action="/someurl" method="post">
<input type="text" name="someName" />
<input type="submit">
</form>

控制器

[HttpPost]
public ActionResult SomeUrl(string someName)
{
...
}

2 个答案:

答案 0 :(得分:7)

你应该使用路线。

您的.html

<form action="{{url('someurl')}}" method="post">
<input type="text" name="someName" />
<input type="submit">
</form>
routes.php

中的

Route::post('someurl', 'YourController@someMethod');

最后在YourController.php

public function someMethod(Request $request)
{
   dd($request->all());  //to check all the datas dumped from the form
   //if your want to get single element,someName in this case
   $someName = $request->someName; 
}

答案 1 :(得分:2)

效果最好

<form action="{{url('someurl')}}" method="post">
 @csrf
<input type="text" name="someName" />
<input type="submit">
</form>

web.php

Route::post('someurl', 'YourController@someMethod');

并在您的控制器中

public function someMethod(Request $request)
{
   dd($request->all());  //to check all the datas dumped from the form
   //if your want to get single element,someName in this case
   $someName = $request->someName; 
}