简体   繁体   English

Laravel 表单发布到控制器

[英]Laravel form post to controller

I am new to Laravel and I'm having trouble with posting data to a controller.我是 Laravel 的新手,无法将数据发布到控制器。 I couldn't find the corresponding documentation.我找不到相应的文档。 I want to something similar in Laravel that I do in C# MVC.我想在 Laravel 中做一些类似于我在 C# MVC 中做的事情。

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

Controller控制器

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

You should use route.你应该使用路由。

your .html你的.html

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

in routes.phproutes.php

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

and finally in YourController.php最后在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; 
}

This works best这效果最好

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

in web.phpweb.php

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

and in your Controller并在您的控制器中

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

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

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