简体   繁体   English

如何在Laravel中使用表单标签

[英]How to use form tag in laravel

I want to use form tag in laravel framework as below 我想在laravel框架中使用表单标签,如下所示

<form method="post" action="<?php echo 'SongController@savenew';?>">

but after submitting it gives me an error 但提交后给我一个错误

TokenMismatchException in VerifyCsrfToken.php line 53: VerifyCsrfToken.php第53行中的TokenMismatchException:

You need to include the CSRF token in your form like this: 您需要像这样在表单中包含CSRF令牌

Laravel makes it easy to protect your application from cross-site request forgeries. Laravel使您可以轻松保护应用程序免受跨站点请求伪造的侵害。 Cross-site request forgeries are a type of malicious exploit whereby unauthorized commands are performed on behalf of the authenticated user. 跨站点请求伪造是一种恶意利用,利用这种手段,代表经过身份验证的用户执行未经授权的命令。

Laravel automatically generates a CSRF "token" for each active user session managed by the application. Laravel为由应用程序管理的每个活动用户会话自动生成CSRF“令牌”。 This token is used to verify that the authenticated user is the one actually making the requests to the application. 此令牌用于验证经过身份验证的用户是实际向应用程序发出请求的用户。 To generate a hidden input field _token containing the CSRF token, you may use the csrf_field helper function: 要生成包含CSRF令牌的隐藏输入字段_token,可以使用csrf_field helper函数:

 <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"> 

You need to use Laravel's form builder. 您需要使用Laravel的表单生成器。 Because it requires a CSRF(Cross-site Request Forgery) token, but when you create the form manually the token isn't added to the html, you have to add the token manually ie. <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"> 因为它需要CSRF(跨站请求伪造)令牌,但是当您手动创建表单时,不会将令牌添加到html中,因此必须手动添加令牌ie. <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"> ie. <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"> , but it is a little safer to use Laravel's standard: ie. <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>"> ,但是使用Laravel标准更安全一些:

<?= Form::open(['controller' => 'SongController@savenew', 'method' => 'POST']); ?>

<?= Form::close(); ?>

If you're using blade( Recommended ): 如果您使用的是刀片( 推荐 ):

{!! Form::open(['controller' => 'SongController@savenew', 'method' => 'POST']) !!}

{!! Form::close(); !!}

Laravel form request always required csrf token Laravel表单请求始终需要CSRF令牌

Used 用过的

<Form method="post" action="">
{{ csrf_field() }}
</Form>

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

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