Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2 Session values not persisting even when web middleware is used

I've got a Laravel 5.2 project that I'm using as a mock API for a javascript client I'm building out. The mock API functionality will be replaced with a different Laravel project later. For now, I just need to be able to submit API calls and get expected responses.

In my mock API I'm storing the authentication status in the session.

The problem I'm running into is that the values I put into the session are not persisting between http calls.

session put is not persisting gif

This seemed similar to a different stackoverflow post I found, but the thing is I'm already using the web middleware for my API group.

I thought it may be a permissions on my storage folder (I'm using the default file session driver), vagrant is the owner and has write access:

storage directory permissions

Plus if it was a permissions issue I would think it would generate a runtime error.

Is there something else I'm missing?

EDIT

Here's the contents of Config::get('session'):

contents of config::get('session')

And yep, the StartSession class is included in the web middleware group:

StartSession class in web middleware group

Here's a shot of the browser session cookie vs the session file being created on the web server:

browser cookie vs session file

Here's the content of the request:

request contents

like image 930
Chris Schmitz Avatar asked Jan 06 '16 19:01

Chris Schmitz


People also ask

How is session maintained in Laravel?

Laravel ships with several great drivers out of the box: file - sessions will be stored in storage/framework/sessions . cookie - sessions will be stored in secure, encrypted cookies. database - sessions will be stored in a database used by your application.

How long does session last Laravel?

How long does laravel session last? You need to understand what happened: You've set the lifetime of the sessions to 120 minutes, which means after 120 minutes the session is flushed. The remember_me feature is using cookies.

Which one is correct to set a session data in Laravel?

The correct syntax for this is: Session::set('variableName', $value); For Laravel 5.4 and later, the correct method to use is put : Session::put('variableName', $value);


2 Answers

I had the same issue and was able to get it to work by replacing

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

with

Route::group(['middlewareGroups' => ['web']], function () {
   ...
});

No idea why this works though when all the documentation suggests that we use ['middleware' => ['web']]

like image 132
howellmartinez Avatar answered Oct 04 '22 22:10

howellmartinez


One thing that did the trick for me was to make sure that domain in config/session.php is set to null.

like image 26
Frisbetarian Avatar answered Oct 04 '22 22:10

Frisbetarian