将 CSRF 令牌添加到 Laravel 表单时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30207737/
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
Error when add CSRF token to Laravel form
提问by ronin1184
I have a login form in Laravel:
我在 Laravel 中有一个登录表单:
<form class="form-signin" action="{{ URL::route('adminAuthen') }}" method="POST">
{{ csrf_field() }}
<h2 class="form-signin-heading">Admin Login</h2>
<label for="inputUsername" class="sr-only">Email address</label>
<input type="text" id="inputUsername" name="username" class="form-control" placeholder="Username" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" id="inputPassword" name="password" class="form-control" placeholder="Password" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
But I always got error when visit this form:
但是我在访问这个表格时总是出错:
Call to undefined function csrf_field()
How can I fix this bug?
我该如何修复这个错误?
回答by Kalhan.Toress
I think there is no function called csrf_field()
in laravel 5, use this instead of that.
我认为csrf_field()
在 laravel 5 中没有调用函数,使用这个而不是那个。
<input type="hidden" name="_token" value="{{ csrf_token() }}">
Update 2016-03-17
更新 2016-03-17
Laravel introduce csrf_field()
in version 5.1
Laravel介绍csrf_field()
中version 5.1
{{ csrf_field() }}
this will generate csrf token field,
{{ csrf_field() }}
这将生成 csrf 令牌字段,
回答by Aditya Giri
You can replace your {{ csrf_field() }}
with this:
你可以{{ csrf_field() }}
用这个替换你的:
<input type="hidden" name="_token" value="{{ csrf_token() }}">
You might have misunderstood this because of master documentation page of laravel. I don't why they did that. But this one is what I found to be working on the laravel-5.
您可能因为 laravel 的主文档页面而误解了这一点。我不明白他们为什么这样做。但这是我在 laravel-5 上发现的。
回答by Alexander Pogor
Update 2018-10-01
in latest versions of laravel use:
更新 2018-10-01
最新版本的 Laravel 使用:
@csrf
and you'll be good to go!
P/S. i use 5.7
你会很高兴去的!
P/S。我用 5.7
回答by Joel Hinz
The function csrf_field()
isn't in regular Laravel 5 yet, even though it's on the master documentation page. You can use the helper csrf_token()
instead, see e.g. the helpers documentation, and then build the field yourself from that - or create a template for it, or similar.
该函数csrf_field()
还没有出现在常规的 Laravel 5 中,即使它在主文档页面上。您可以改用助手csrf_token()
,例如参见助手文档,然后自己从中构建字段 - 或为其创建模板,或类似的。