将CSRF令牌添加到Laravel表单时出错

时间:2015-05-13 06:57:50

标签: laravel laravel-5

我在Laravel中有一个登录表单:

  protected function button2_clickHandler(event:MouseEvent):void
  {
        for(var n:int = 0; n < 100; n = n+20){
        progressBar.setProgress(n, 100);
        progressBar.label = "Hello World " + n;
        // progressBar.validateNow(); 
        }
  }
  <mx:VBox width="100%" height="100%">
        <mx:ProgressBar id="progressBar"/>
        <mx:Button label="Update Progress" click="button2_clickHandler(event)"/>                
    </mx:VBox>

但是访问此表单时我总是遇到错误:

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

如何修复此错误?

4 个答案:

答案 0 :(得分:19)

我认为laravel 5中没有名为csrf_field()的函数,请使用此函数代替。

<input type="hidden" name="_token" value="{{ csrf_token() }}">

更新2016-03-17

Laravel在version 5.1

中介绍csrf_field()

{{ csrf_field() }}这将生成csrf标记字段

答案 1 :(得分:3)

您可以将{{ csrf_field() }}替换为:

<input type="hidden" name="_token" value="{{ csrf_token() }}">

由于laravel的主文档页面,您可能误解了这一点。我不知道他们为什么那样做。但是这个是我发现在laravel-5上工作的。

答案 2 :(得分:2)

更新2018-10-01

在最新版本的laravel中使用:

@csrf

您会很高兴!
P / S。我使用5.7

答案 3 :(得分:1)

函数csrf_field()在常规Laravel 5中尚未定义,即使它位于master documentation page上。您可以使用帮助csrf_token()代替,例如, helpers documentation,然后自己构建字段 - 或为其创建模板或类似字段。