2016-03-08 75 views
0

我偶爾TokenMismatchExceptions的問題,所以我決定向Laravel 5.1重定向withErrors()空鑑於

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.']); 
} 

添加到應用程序/例外/ Handler.php文件中render功能。

我嘗試了許多不同的方式來重定向,並且視圖從不顯示錯誤。如果我有不正確的登錄信息,視圖將顯示錯誤。

如果我殺死了腳本並轉儲了會話,我可以看到這些錯誤,但是如果我在視圖中轉儲會話,那麼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\[email protected]'); 
Route::post('auth/login', 'Auth\[email protected]'); 
Route::get('auth/logout', 'Auth\[email protected]'); 

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

+0

將'{{$ error}}'更改爲'{!! $ error !!}' – aldrin27

+0

我把這個變量放在視圖中,它是空的 – Ethan22

+0

這是在你的視圖var_dump($ errors)'? – aldrin27

回答

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

什麼都沒有。 'var_dump(Session:all())'仍然只顯示_token – Ethan22