Sessions not persistent in the Lumen PHP framework

I use the Lumen microstructure (by Laravel) for the project, and I am having problems with sessions. I am just testing the implementation now, but the problem I'm experiencing is that when I set the session variable and then refresh the page, the variable is no longer set.

In my .env file, I have:

SESSION_DRIVER=cookie 

And I know that this is matched because when I change it to memcached it generates an error (because I don't have memcached settings).

I also included middleware:

 $app->middleware([ 'Illuminate\Session\Middleware\StartSession', 'Illuminate\View\Middleware\ShareErrorsFromSession', ]); 

Then in my controller, I:

 <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Controllers\Controller; class SessionController extends Controller { public function index(Request $request) { $request->session()->put('email', ' test@test.com '); $request->session()->save(); // Not sure if this is required var_dump($request->session()->get('email')); exit; return view('session.index', ['test' => $value]); } } 

The value is set when the page loads:

 string(13) " test@test.com " 

But then, when I comment out the lines that set the variable and then refresh the page, the value is NULL:

 // $request->session()->put('email', ' test@test.com '); // $request->session()->save(); var_dump($request->session()->get('email')); exit; 

The browser sets one cookie, but it does not appear for the session variable:

 laravel_session 2ecef0103418ca82d068ec6a6c6fbec388af9b9e localhost / 2015-06-22T14:59:29.856Z 55 

EDIT: The cookie is actually set if I set SESSION_DRIVER as a cookie - regardless of whether I actually set the session variable.

I am not sure where I am going wrong, and I do not find the documentation very comprehensive.

thanks

+6
source share
1 answer

Since you are trying to use the cookie session store, you will also need cookie middleware. Allowed middleware must be:

 $app->middleware([ 'Illuminate\Cookie\Middleware\EncryptCookies', 'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse', 'Illuminate\Session\Middleware\StartSession', 'Illuminate\View\Middleware\ShareErrorsFromSession', ]); 

In the first example, you use the session immediately after creating it to restore it. However, since the session cookie itself is not set by the code, when you return to the page, the session is not restored. The cookie "laravel_session" is always set whether you use the cookie store or not. I use file storage in the project and still get it.

Also, AFAIK, line:

 $request->session()->save(); 

not required.


The documentation states:

To enable sessions, you must uncomment all calls to the $app->middleware() method in your bootstrap/app.php .

+2
source

Source: https://habr.com/ru/post/989527/


All Articles