Laravel back()->withInput(); 不工作

0 redirect login laravel

我目前正在关注登录表单上的视频教程。这是 Auth::attempt() 失败时使用的代码:

return back()->withInput();
Run Code Online (Sandbox Code Playgroud)

这应该将用户返回到表单并再次填写输入(电子邮件、密码)。然而,这些字段保持为空,而登录正常工作。

我怎样才能解决这个问题?

这是我的表格:

{!! Form::open(array('route' => 'handleLogin')) !!}
        <div class="form-group has-feedback">
            {!! Form::text('email', null, array('class' => 'form-control', 'placeholder' => 'EMail')) !!}
            <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
        </div>
        <div class="form-group has-feedback">
            {!! Form::password('password', array('class' => 'form-control', 'placeholder' => 'Password')) !!}
            <span class="glyphicon glyphicon-lock form-control-feedback"></span>
        </div>
        <div class="row">
            <div class="col-xs-8">
                <div class="checkbox icheck">
                    <label>
                        <input type="checkbox"> Remember Me
                    </label>
                </div>
            </div>
            <!-- /.col -->
            <div class="col-xs-4">
                {!! Form::token() !!}
                {!! Form::submit(null, array('class' => 'btn btn-primary btn-block btn-flat')) !!}
            </div>
            <!-- /.col -->
        </div>
        {!! Form::close() !!}
Run Code Online (Sandbox Code Playgroud)

编辑:发现以下代码适用于登录,但当登录凭据错误时不会执行 else 语句。为什么?

public function handleLogin(Request $request)
    {
        $this->validate($request, User::$login_validation_rules);
        $data = $request->only('email', 'password');
        if(\Auth::attempt($data)){
            return redirect()->intended('home');
        }else {
            return back()->withInput();
        }
    }
Run Code Online (Sandbox Code Playgroud)

res*_*all 5

因此,正如我们所讨论的,问题在于web中间件。在 Laravel 5.2 框架的先前版本中,如果您想启用会话和错误变量,则必须将路由包装在web中间件中。

从 version 开始5.2.27,情况不再如此,因为默认情况下,该中间件组已经应用于所有路由。

众所周知,使用版本5.2.27或更高版本,并使用 web 中间件组,会导致相同变量的问题,常见问题是会话变量没有被传递,并且$errors设置了\Illuminate\View\Middleware\ShareErrorsFromSession由 Laravel 验证 API 返回的类提供的变量,但空**。

概括

  • 如果您有Laravel 5.2.27 及更高版本,则无需将路由包装在web中间件组中。
  • 如果版本低于该版本,那么您需要这样做,以便使用会话变量并获得验证错误。

来源