Laravel中间件不存储会话值

时间:2017-03-30 06:25:44

标签: php laravel session laravel-5 middleware

我尝试将我的语言放在session()中,但是当我检查会话时:dd(session()->get('locale')); - 它是空的。我不知道为什么。我需要在会话中存储该值。

我的中间件:

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);
    }
}

我的内核:

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,
    ],

1 个答案:

答案 0 :(得分:0)

让我们试试这个

它会起作用

\Session::save();

每当您想要保存修改时,我们就要说我想存储用户的语言

\Session::put('lang','en');
\Session::save();