Skip to content
Advertisement

Laravel Middleware not executing functions

I have this Locale middleware which sets language based from settings on DB

    public function handle($request, Closure $next)
    {
        $HQ = Branch::where('is_hq', 1)->where('is_active', 1)->first();

        $Company = GlobalVariable()->branch($HQ)->all()->whereIn('group', array(1, 3, 9))->keyBy('key');
        $locale = strtolower($Company['bi__language']->value);

        if ($locale === 'eng') {
            App::setlocale('en');
        } else {
            App::setlocale($locale);
        }

        return $next($request);
    }

but i need to get the branch where the current user is so i need to get the Auth::id() first so i changed my code to this to access Auth.

     public function handle($request, Closure $next)
     {
        $response = $next($request);

        $HQ = Branch::where('is_hq', 1)->where('is_active', 1)->where('user_id', Auth::id())->first();

        $Company = GlobalVariable()->branch($HQ)->all()->whereIn('group', array(1, 3, 9))->keyBy('key');
        $locale = strtolower($Company['bi__language']->value);

        if ($locale === 'eng') {
            App::setlocale('en');
        } else {
            App::setlocale($locale);
        }

        return $response;
    }

Now i am getting the current user logged in but the problem is it is not executing this block of code

if ($locale === 'eng') {
   App::setlocale('en');
} else {
    App::setlocale($locale);
}

Kernel.php

protected $middleware = [
    IlluminateFoundationHttpMiddlewareCheckForMaintenanceMode::class,
    IlluminateFoundationHttpMiddlewareValidatePostSize::class,
    AppHttpMiddlewareTrimStrings::class,
    IlluminateFoundationHttpMiddlewareConvertEmptyStringsToNull::class,
    AppHttpMiddlewareTrustProxies::class,
    BarryvdhCorsHandleCors::class,
    AppHttpMiddlewareLocale::class,
];

Note: the code wihout Auth is executing this block of code but the second isn’t

Am i missing something here?

Advertisement

Answer

For the second one you have

$response = $next($request);

at the very beginning, so the code will execute at response.

You need to do something like this:

    public function handle($request, Closure $next)
    {
        $HQ = Branch::where('is_hq', 1)->where('is_active', 1)->where('user_id', Auth::id())->first();
        $Company = GlobalVariable()->branch($HQ)->all()->whereIn('group', array(1, 3, 9))->keyBy('key');
        $locale = strtolower($Company['bi__language']->value);

        if ($locale === 'eng') {
            App::setlocale('en');
        } else {
            App::setlocale($locale);
        }

        return $next($request);
    }

If you do not have the auth()->user(), this is because the place that you register the middleware is not correct.

The global middleware stack runs prior to the session being started and authentication details being available.

Define this at the bottom of the ‘web’ group or in your route middleware.

    protected $middlewareGroups = [
        'web' => [
            AppHttpMiddlewareEncryptCookies::class,
            IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
            IlluminateSessionMiddlewareStartSession::class,
            // IlluminateSessionMiddlewareAuthenticateSession::class,
            IlluminateViewMiddlewareShareErrorsFromSession::class,
            AppHttpMiddlewareVerifyCsrfToken::class,
            IlluminateRoutingMiddlewareSubstituteBindings::class,
            AppHttpMiddlewareLocale::class,
        ],

        'api' => [
            'throttle:60,1',
            IlluminateRoutingMiddlewareSubstituteBindings::class,
        ],
    ];
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement