jeudi 30 mars 2017

Laravel middleware doesn't store the session value

I try to put my language in session(), but when I check session: dd(session()->get('locale')); - it's null. I have no idea why. I need to store that value in session.

my middleware:

class LangSwitch {
...
public function handle(Request $request, Closure $next)
        if(!session()->has('locale')){
            $locale = $request->segment(1);
            session()->put('locale', $locale);    
        }
        else{
            $locale = session()->get('locale');
        }

        if (!array_key_exists($locale, Config::get('app.locales'))) {
            $segments = $request->segments();
            $segments[0] = Config::get('app.fallback_locale');

            return redirect()->to(implode('/', $segments));
        }

        App::setlocale($locale);

        return $next($request);
    }
}

my kernel:

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,
        \App\Http\Middleware\LangSwitch::class,
    ],



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire