Marco
Marco

Reputation: 789

Session is not written in Laravel 5

I am looking for kind help.

I make a fresh installation of Laravel (5.2.15 by this time). I'm not using Homested. I chmod 'storage' directory and subdirectories, as documentation suggests, giving write permission. Session is configured as default, did not touch anything. So session driver is 'file' and path is 'framework/sessions'.

I write these test routes:

Route::get('/', function () {
    session(["test" => "ok"]);
    return view('welcome');
});

Route::get('/2', function () {
    print "session = ".session('test');exit();
});

Shouldn't it be enough to see session working? session('test') is null, and no file is written in framework/sessions. Chmoded 777 framework/sessions folder to be sure, nothing changed.

I'm a newbie at Laravel.. maybe I am missing something? Do i need to set these vars in config/session.php?

'path' => '/',
'domain' => null,

Thank you

Upvotes: 1

Views: 1632

Answers (2)

Denis Mysenko
Denis Mysenko

Reputation: 6534

If you open your app/Http/Kernel.php, you will see that session class is a part of 'web' group, so it's not included by default:

 protected $middlewareGroups = [
        'web' => [
        ...
            \Illuminate\Session\Middleware\StartSession::class,
        ...
        ]
 ]

Therefore, you have to wrap your routes in a group with 'web' middleware or add 'web' middleware to each route separately:

Route::group(['middleware' => ['web']], function() {
    Route::get('/', function () {
        session()->put('test', 'ok');
        return view('welcome');
    });

    Route::get('/2', function () {
        return 'session = ' . session('test');
   });
}

Upvotes: 5

BKF
BKF

Reputation: 1366

Try this :

Route::get('/', function () {
    Session::put('test', 'ok');
    return view('welcome');
});

Route::get('/2', function () {
    print "session = ".Session::get('test');exit();
});

Upvotes: 2

Related Questions