0
votes

I have a global middleware that checks if the app is in development mode, and if it is it returns a login form view, this view validates the login and then uses the errors variable to display any validation errors:

App\Http\Kernel

/**
 * 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 variable in my view

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

This worked great in Laravel 5.2, but when I updated to L5.4 the session and the error sharing gets instantiated in the web middleware group so now in L5.4 there is no access to the session in my global middleware.

/**
 * 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',
    ],
];

How do I manually instantiate a new session in my global middleware so that I can use the $errors variable for validation?

1
How about checking if the error exist in the first place $errors->has("email")paudel
@Paudel I am already checking for that with $errors->first(), the problem is that the $errors variable that laravel includes in all views by default is not available in my global middleware as laravel starts the session and shares the error variable in the web middleware group.enriqg9
How does your route looks like?Basheer Kharoti
@BasheerAhmed I have quite a lot of routes so that is why I did not include them, but do they really matter? If i'm not mistaken, Global middleware runs before the routes are even accessed.enriqg9
we just need to know that which middleware your routes are actually using?Basheer Kharoti

1 Answers

1
votes

I found that the easiest way was to start the session and share the errors on the global middleware so that we can get access to the $errors variable in the view. If anybody recommends another solution I will change the accepted answer.

/**
 * 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,
];