Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mrshoghi's avatar

Session Data Not Saving When Called in Router, I want to know why!

I have this simple code in routes.php. It is meant to switch language to Spanish. But saved information in the session is gone by the next page load and session is empty.

$language = Session::get('language',Config::get('app.locale')); App::setLocale($language);

Route::get('/', function () { return view('page.home'); })->name('home');

Route::get('contact', function () { return view('page.contact'); })->name('contact');

Route::get('switchtospanish', function () { //session(['language' => 'es']); Session::put('language','es'); Session::save(); return back(); })->name('switchtospanish');

There aren't any errors. But I do not understand why is this not working. I saw different ways to do this online (like making a middleware) but to better understand, I want to know why is this not working.

0 likes
2 replies
mrshoghi's avatar

The first line always returns 'en' as it is the default. When I do Session:get('language') returns null.

mrshoghi's avatar

When I looked for the session file, it is created, and the 'language' value is set to 'es'.

So I guess saving works, but the first line that reads the session doesn't.

Please or to participate in this conversation.