2017-04-11 106 views
0

我有一個全局中間件,用於檢查應用程序是否處於開發模式,如果是,則返回登錄表單視圖,此視圖驗證登錄,然後使用errors變量顯示任何驗證錯誤:

應用程序\ HTTP \內核在我看來

/** 
* The application's global HTTP middleware stack. 
* 
* @var array 
*/ 
protected $middleware = [ 
    \App\Http\Middleware\CheckForDevelopmentMode::class, 
    \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, 
    \App\Http\Middleware\TrimStrings::class, 
    \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, 
]; 

$錯誤變量

{!! $errors->first('email', '<span class="help-block">:message</span>') !!} 

這Laravel 5.2偉大的工作,但瓦特當我更新到L5.4會話並且錯誤共享在網絡中間件組中被實例化時,所以現在在L5.4中,我的全局中間件中沒有訪問會話。

/** 
* The application's route middleware groups. 
* 
* @var array 
*/ 
protected $middlewareGroups = [ 
    'web' => [ 
     \App\Http\Middleware\EncryptCookies::class, 
     \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, 
     \Illuminate\Session\Middleware\StartSession::class, 
     \Illuminate\View\Middleware\ShareErrorsFromSession::class, 
     \App\Http\Middleware\VerifyCsrfToken::class, 
     \Illuminate\Routing\Middleware\SubstituteBindings::class, 
    ], 
    'api' => [ 
     'throttle:60,1', 
     'bindings', 
    ], 
]; 

如何在全局中間件中手動實例化新會話,以便我可以使用$ errors變量進行驗證?

+0

如何檢查,如果擺在首位$錯誤 - 存在錯誤>的(「郵件」) – Paudel

+0

@Paudel我已經用'$錯誤 - ()'檢查該>首先,問題是默認情況下,laravel在所有視圖中包含的$ errors變量在我的全局中間件中不可用,因爲laravel啓動會話並在Web中間件組中共享錯誤變量。 – enriqg9

+0

你的路線是怎樣的? –

回答

0

我發現最簡單的方法是啓動會話並在全局中間件上共享錯誤,以便我們可以訪問視圖中的$ errors變量。如果有人推薦另一種解決方案,我會改變接受的答案。

/** 
* The application's global HTTP middleware stack. 
* 
* @var array 
*/ 
protected $middleware = [ 
    // Start the session and share errors globally so that we can access the 
    // errors variable in the development mode view. 
    \Illuminate\Session\Middleware\StartSession::class, 
    \Illuminate\View\Middleware\ShareErrorsFromSession::class, 
    \App\Http\Middleware\CheckForDevelopmentMode::class, 
    \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, 
    \App\Http\Middleware\TrimStrings::class, 
    \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, 
];