How to enable session in Lumen 5.4

Having a problem on how to enable the session in Lumen? Well I've been in that situation few weeks ago, I didn't know that laravel has officially stopped supporting session in laravel/lumen framework from version 5.2 on wards before I've started my project.

Anyway here's what I did to enable session in lumen.

Step 1:

Create a config directory in Lumen root directory, copy the session.php from Laravel's config folder to the created config folder in Lumen

 

Step 2:

Load session config, open bootstrap/app.php

Add this line of code in the area of the register middleware

$app->configure('session');

 

Step 3:

Bind the session manager

$app->bind(Illuminate\Session\SessionManager::class, function ($app) {    
    return $app->make('session');
});

Add the start session middleware

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

Then register the session manager

$app->register(Illuminate\Session\SessionServiceProvider::class);

 

Step 4:

Create the "sessions" folder where laravel store it lumen/storage/framework/sessions

Lastly open the .env file and change the value of SESSION_DRIVER to file

That's it! you can now use session again through the request class!

E.g. from any of your controller file

just import it

use Illuminate\Http\Request;

then from any method in controller

public function yourMethod(Request $request) {
	$session = $request->session();
	$session->put('sample','hello');
	echo $session->get('sample');  // return hello
}

Hope that helps!