2017-11-03 102 views
2

這裏是我的login.blade.phpLaravel 5.5登錄錯誤沒有顯示出來

@if(Session::get('errors')||count($errors) > 0) 
    @foreach ($errors->all() as $error) 
     <h1>{{ $error }}</h1> 
    @endforeach 
@endif 

這裏是我的LoginController.php:

protected function sendFailedLoginResponse(Request $request) 
{ 
    return redirect()->back() 
     ->withInput($request->only($this->username(), 'remember')) 
     ->withErrors([ 
      $this->username() => 'ERRORS', 
     ]); 
} 

這裏是我的web.php(路線)

// I am customizing the login to do extra checks, 
// but I still need the basic auth scaffolding. 
Auth::routes(); 
... 
Route::group(['middleware' => 'web'], function() { 
    Route::view('/login', 'auth.login'); 
    Route::post('/login', 'Auth\[email protected]')->name('login'); 
}); 

當我試圖用錯誤的用戶它顯示視圖中沒有錯誤登錄,我算什麼我做錯了?

更新
我試圖改變login.blade.php,作爲@Seva卡拉什尼科夫認爲,沒有運氣。
我也試過@Akshay Kulkarni建議沒有運氣。

回答

1

好吧,幾個小時後我終於找到了!我創建了一個從無到有的Laravel項目,並提出了差異找到罪魁禍首:

應用程序/ HTTP/Kernel.php,一定要擺脫StartSession中間件:

protected $middleware = [ 
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, 
    \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, 
    \App\Http\Middleware\TrimStrings::class, 
    \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, 
    \App\Http\Middleware\TrustProxies::class, 
    \Illuminate\Session\Middleware\StartSession::class, // <-- Remove this 
]; 

說明:我在那裏,因爲我讀過,我不得不把它作爲中間件(如果我沒有在我的web.php中使用Route::group(['middleware' =>'web']包裝),我認爲我忘了它。我認爲把它放在那裏使用web.php中的包裝以某種方式截斷錯誤會話,然後它進入視圖。

0

嘗試從if聲明login.blade.php刪除Session::get('errors')

@if(count($errors) > 0) 
    @foreach ($errors->all() as $error) 
     <h1>{{ $error }}</h1> 
    @endforeach 
@endif 

ShareErrorsFromSession中間件,它是由Web中間件組提供負責$error視圖變量,因此它總是被定義(link here

[更新]

而作爲@Ohgodwhy指出,你需要使用@if ($errors->any())Example

所以你的情況將是:

@if($errors->any()) 
    @foreach ($errors->all() as $error) 
     <h1>{{ $error }}</h1> 
    @endforeach 
@endif 
+0

它應該是'$ errors-> any()' – Ohgodwhy

+0

下面是來自Laravel Basic Auth Views的示例:https://github.com/drbyte/laravel-basic-auth-views/blob/master/resources/views /auth/login.blade.php –

+0

和[這裏是來自documentaion的示例](https://laravel.com/docs/5.5/validation#quick-displaying-the-validation-errors)。它應該是'$ errors-> any()'。查看留言包始終可用。 – Ohgodwhy

0

說,

驗證::路線();

內部中間件組。

Web中間件啓動會話。 如果您正在編寫中間件組之外的任何路由,則無法訪問會話。

+0

這也沒有幫助,我沒有得到任何錯誤。 – funerr

0

如果您使用Entrust(或者其他一些軟件包)並將它們的類添加到$routeMiddleware,則問題可能源於隨後添加的自定義類覆蓋默認的Laravel類。

解決方案是將您的自定義類移動到$routeMiddleware數組的頂部。