Laravel会话闪存不适用于auth路由

时间:2017-01-17 20:17:36

标签: laravel laravel-5.3

当存在CSRF令牌不匹配时,我正尝试使用有用的消息重定向回登录表单。为此,我将以下内容添加到Exceptions\Handler

public function render($request, Exception $exception)
{
    if ($exception instanceof TokenMismatchException) {
        return redirect('/')->withErrors([
            'token_mismatch' => trans('auth.token_mismatch')
        ]);
    }

    return parent::render($request, $exception);
}

但是,该消息从未进入视图中的$errors变量:

@if (count($errors))
    <div class="alert alert-danger alert-dismissable margin5">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
        {{ $errors->first() }}
    </div>
@endif

据我所知,我的Http\Kernel看起来不错:

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],
];

我的web.php文件包含用于处理登录的Laravel 5.3默认Auth::routes();

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

我能够通过改变来解决这个问题:

return redirect('/')->withErrors([
    'token_mismatch' => trans('auth.token_mismatch', ['domain' => config('session.domain')])
]);

...为:

return redirect()->back()->withErrors([
    'token_mismatch' => trans('auth.token_mismatch', ['domain' => config('session.domain')])
]);