简体   繁体   English

无法从Laravel中的表单获取数据?

[英]Can not get my data from my form in Laravel?

I am new to Laravel, I am trying to have a simple example, but I am getting a 419 error, I dont know why it shows up but I will expalin what I did, I created a simple Controller and I called it FormController with the command line : 我是Laravel的新手,我想举一个简单的示例,但出现419错误,我不知道为什么会出现,但是我会解释我所做的事情,创建了一个简单的Controller,并使用命令行 :

 php artisan make:controller --resource FormController

In my web.php I added this : 在我的web.php中,我添加了以下内容:

Route::resource('form','FormController');

my view has a simple form in it : 我的看法有一个简单的形式:

<form action="/form" method="POST" >
   <input type="text" name="cih">
   <input type="submit">
</form>

I open my view with the create method : 我用create方法打开视图:

public function create()
{
    return view('contact');
}

I want that when I submit my form I get my data, so I use my 'store' method : 我希望在提交表单时得到我的数据,所以我使用了“存储”方法:

public function store(Request $request)
{
    return $request->all();
}

But instead of getting it, I get 419 message, and my session has expired etc .. 但是没有得到它,而是得到419消息,并且我的会话已过期等。

I followed a course and that what the teacher was doing I believe nothing more, so I would appreciate any help, I need it. 我参加了一个课程,而老师正在做什么,我只相信了一点,因此,我需要任何帮助,我将不胜感激。

Thank you 谢谢

You need to include the CSRF token while submitting a form since the 'VerifyCsrfToken' middleware is enabled by default for the web routes in App/Http/Kernal.php . 提交表单时,您需要包括CSRF令牌,因为默认情况下, App/Http/Kernal.php的Web路由启用了“ VerifyCsrfToken”中间件。

<form action="/form" method="POST" >
   @csrf
   <input type="text" name="cih">
   <input type="submit">
</form>

And, Welcome to Laravel! 并且,欢迎来到Laravel!

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

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