首页 文章

Laravel 5.1重定向withErrors()在视图中为空

提问于
浏览
0

我偶尔会遇到TokenMismatchExceptions的问题所以我决定添加

if($e instanceof TokenMismatchException)
{
    return \Redirect::to('auth/login')->withErrors(['Seems like you may have waited to long to use this application. Please refresh and try again.']);
}

render 函数中的app / Exceptions / Handler.php文件 .

我尝试了许多不同的重定向方式,视图从不显示错误 . 如果我的登录信息不正确,视图将显示错误 .

如果我终止脚本并转储会话,我可以看到错误但是如果我在视图中转储会话,则errors对象为空 .

视图

@if (isset($errors) && count($errors) > 0)
   <div class="alert alert-danger">
       <ul>
           @foreach ($errors->all() as $error)
              <li>{{ $error }}</li>
           @endforeach
       </ul>
   </div>
@endif

路线

Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');

/**
 * Need to be logged in to access all of these routes.
 */
Route::group(['middleware' => 'auth'], function(){
    Route::get('/', function () {
        return Redirect::to('/home');
    });
});

Laravel 5.1

1 回答

  • 0

    试试这个:

    return redirect('auth/login')->with('errors', ['Seems like you may have waited to long to use this application. Please refresh and try again.']);
    

    然后在你看来:

    @if (session('errors'))
        <div class="alert alert-danger">
            <ul>
                @foreach (session('errors') as $error)
                  <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
    

相关问题