I am trying to implement multilingualism on the site under laravel 5.4.

Locale to show

Route::get('setlocale/{locale}', 'HomeController@language'); public function language($locale){ if (in_array($locale, \Config::get('app.locales'))) { // Проверяем, если у пользователя выбран доступный язык Session::put('locale', $locale); // Устанавливаем его в сессии под именем locale } return Redirect::back(); } 

If in the blade insert {{\Session::get('locale')}} , then I get the desired value (say ru)

And if you insert such code in __construct

 if (Session::get('locale') != null) { $lang = Session::get('locale'); } else if (Session::get('locale') == null) { $lang = "null"; } View::share('lang', $lang); 

, I get null. I break my head, I can not understand why!?

Naturally in the controller are connected

 use View; use Session; 

Help please understand why __construct does not give the value of Session::get('locale') . thanks in advance

    1 answer 1

    The class constructor has not yet been processed by Middleware, which Session and Cookies understand, which is why it is empty.

    The order of processing, if interested, you can see in the file Kernel.php

    as an option to do so

     public function __construct() { $this->middleware(function ($request, $next) { $this->locale = session()->has( 'locale' ) ? session()->get( 'locale ) : 'ru'; return $next($request); }); }