session not persisting in laravel

Asked by At

I use Session in laravel, but when I get session in controller, it's not work.

web.php

Route::get('locale/{locale}',function ($locale){
    //Session::put('locale',$locale);
    session(['locale' => $locale]);
    Session::save();
    return redirect()->back();
});

Localization.php

public function handle($request, Closure $next)
{
    if(\Session::has('locale')){
        \App::setLocale(\Session::get('locale'));
        session(['locale' => \Session::get('locale')]);
        \Session::save();
    }

    return $next($request);
}

Kernel.php

protected $middleware = [
    \App\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
    \App\Http\Middleware\TrimStrings::class,
    \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    \App\Http\Middleware\TrustProxies::class,
];

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

    'api' => [
        \App\Http\Middleware\EventLogMiddle::class,
        'throttle:600,1',
        'bindings',
    ],
];

MachCtrl.php (extends Controller)

public function index()
{
    $lang = session('locale');
    $config = json_encode([
        "table" => "Machine_list",
        "title" => "machine list",
        "rows" => 20,
        "column" => [
            "Machine_id", "VM_name", "Machine_location","IP_address", 
            "Network_status", "Current_status"
        ],
        "CUDP" => "0111",
        "CreateRoute" => route('machine.create'),
        "DeleteRoute" => route('machine.destroy'),
        "UpdateRoute" => route('machine.editInfo'),
        "locale" => $lang,
    ]);

    return view('table.show')->with(['config' => $config, 'lang' => 
                                                            $lang]);
}

I save Session in router and middlemare, it's not work.

But when I put Session in Controller, it's work. As following code:

public function index()
{
    session(['locale' => 'en']);
    $lang = session('locale');
    $config = json_encode([
        "table" => "Machine_list",
        "title" => "machine list",
        "rows" => 20,
        "column" => [
            "Machine_id", "VM_name", "Machine_location","IP_address", 
            "Network_status", "Current_status"
        ],
        "CUDP" => "0111",
        "CreateRoute" => route('machine.create'),
        "DeleteRoute" => route('machine.destroy'),
        "UpdateRoute" => route('machine.editInfo'),
        "locale" => $lang,
    ]);

    return view('table.show')->with(['config' => $config, 'lang' => 
                                                           $lang]);
}

I really want to know what happen in my project. Thanks.

Edit: I use dd(session()) in my code:

-web.php

Route::get('locale/{locale}',function ($locale){
    Session::put('locale',$locale);
    Session::save();
    dd(session());
    return redirect()->back();
});

Result:

enter image description here

-Localization.php

public function handle($request, Closure $next)
{


    if(\Session::has('locale')){
        \App::setLocale(\Session::get('locale'));
        session(['locale' => \Session::get('locale')]);
        //dd(session());
        \Session::save();

    }

    return $next($request);
}

Result:

enter image description here

-MachCtrl.php

public function index()
{
    dd(session());
    $lang = session('locale');
    $config = json_encode([
        "table" => "Machine_list",
        "title" => "machine list",
        "rows" => 20,
        "column" => [
            "Machine_id", "VM_name", "Machine_location","IP_address", 
            "Network_status", "Current_status"
        ],
        "CUDP" => "0111",
        "CreateRoute" => route('machine.create'),
        "DeleteRoute" => route('machine.destroy'),
        "UpdateRoute" => route('machine.editInfo'),
        "locale" => $lang,
    ]);

    return view('table.show')->with(['config' => $config, 'lang' => 
                                                            $lang]);
}

Result:enter image description here

2 Answers

1
stokoe0990 On

Try using a different session driver to round down your issue a little bit.

EDIT: Sorry I can't comment yet, <50rep :(

1
senty On

I checked my working code. I set the whole cycle as:

Language middleware:

class Language
{
    protected $app;

    public function __construct(Application $app, Request $request) {
        $this->app = $app;
    }

    public function handle($request, Closure $next)
    {
        $this->app->setLocale(session('user_locale', config('app.locale')));

        return $next($request); 
    }
}

In my middleware Kernel.php:

protected $middlewareGroups = [
    'web' => [
         \Illuminate\Session\Middleware\StartSession::class,
         //...
         \App\Http\Middleware\Language::class,
    ]
];

And in route:

Route::get('locale/{locale}',function ($locale){
     session(['user_locale' => $locale]);
});