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

jason100's avatar

Why am I losing Session?

Laravel 5.2 Firefox .. same issue on Chrome
'driver' => env('SESSION_DRIVER', 'file'),
'lifetime' => 120,
'expire_on_close' => false,

I have a page where the user logs in and I save Session with a final Session::save(); for good measure, and then display home.blade.php where I have put:

$data = Session::all();
var_dump($data);

Which gives me a reassuring dump of the Session variables and I can use those session vars in my template -- so session is working. There is also a link on home.blade.php to
Route::get('timesheet', 'Timesheet\TimesheetController@index');
And there, I've put :

use Illuminate\Support\Facades\Session;

class TimesheetController extends Controller{
    public function index(){
        $data2 = Session::all();
        var_dump($data2);

But all it gives me is:

array(0) {
}

How did I lose my session?

0 likes
5 replies
jason100's avatar
// Timesheet routes...
Route::get('timesheet', 'Timesheet\TimesheetController@index');
.
.
.
.
Route::group(['middleware' => ['web']], function () {
    // Registration routes...
    Route::get('auth/register', 'Auth\AuthController@getRegister');
    Route::post('auth/register', 'Auth\AuthController@postRegister');
    
    Route::get('/dashboard', function(){
        return view('home');
    });

AuthController has "protected $redirectPath = '/dashboard';" after registration (and 'push'ing vars into Session)
Which takes me to home.blade.php where I can dump, and see, the Session vars
home.blade.php has href: < href="timesheet"> Which takes me to Timesheet\TimesheetController@index where my Session vars don't exist anymore

ChristopherSFSD's avatar
Level 4

Timesheet isn't wrapped in web middleware. Only web middleware maintains state (sets a session)

jason100's avatar

OOOOOhhhh!!
It works! That, it brings up the question:
Why wouldn't I put pretty much put every web route in web middleware? What do I gain from putting web-based routes OUTSIDE of web middleware?

thomaskim's avatar

You put everything in the web middleware. Routes outside of it is for things like APIs.

1 like

Please or to participate in this conversation.