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

kayintveen's avatar

Sessions not loading from session files Laravel 5.2

Im pulling my hair out, i tried everything so far but nothing is working. i have 2 very simple functions inside a basic controller. one root route is setting a value and the other is printing the value (directly in the controller for test purposes.

Sessions get stored / saved in the correct folder. but with every refresh a new files gets added with the exact same content. Whats going wrong, i see a lot of topics online about sessions. its so basic but still can cause so much trouble apperantly.

    public function home()
    {
        Session::set('aa','bb');
        Session::save();
    }
    public function all()
    {
        echo print_r(Session::all());
    }

by the way i tried it with set and put, both as 2 values like the example now or in a array. i also tried adding use Illuminate\Support\Facades\Session; or use Session;

working on homestead.

0 likes
1 reply
kayintveen's avatar
kayintveen
OP
Best Answer
Level 5

Sorry i just replied in another topic with somebody facing i thought was the same issue but i was using 5.2 and not 5.1 This is what i wrote and i solved it for now!


I think i found the solution, and its stupid of me, but i needed to read a lot better. You have to wrap your routes around in the web middleware like this

Route::group(['middleware' => ['web']], function () {
    //your routes here.
});

The web middleware is stated in the app/http/Kernel.php see this

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

        'api' => [
            'throttle:60,1',
        ],
    ];

you see that there is a line, which made things really clear for me

            \Illuminate\Session\Middleware\StartSession::class,

Thats why it was not persisting. Good luck!

Please or to participate in this conversation.