Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling session in lumen framework

I have two (but let's image more) micro-services (API) which need to be aware of authenticated user. Ideally I would simple like to resume their sessions.

All micro-services are using same storage for sessions: redis.

All API calls will have Cookie header, so all services will be able to resume sessions based on that cookie. I have successfully implemented this via PHP $_SESSIONs.

Now the question: how would you go about implementing this with Laravel/Lumen?

like image 698
rock3t Avatar asked Nov 01 '17 08:11

rock3t


People also ask

Why is lumen faster than Laravel?

The number of requests handled by Laravel is higher than that of Lumen. This is the reason why Lumen is the fastest Micro Framework whereas Laravel is much slower. To be precise, Lumen handles 100 requests per second. So, if speed is your requirement, then you know which framework to choose.

How does session work in Laravel?

Sessions are used to store information about the user across the requests. Laravel provides various drivers like file, cookie, apc, array, Memcached, Redis, and database to handle session data. By default, file driver is used because it is lightweight. Session can be configured in the file stored at config/session.

What is difference between Laravel and Lumen?

Differences between Laravel and Lumen: Laravel is an MVC based full-stack web application framework which supports a lot of third-party tools like Spatie, Entrust, Socialite etc and frameworks. Lumen is a micro framework, used to develop micro-services and API development in high speed and less time.

What is Laravel lumen framework?

Lumen is an open-source PHP micro-framework created by Taylor Otwell as an alternative to Laravel to meet the demand of lightweight installations that are faster than existing PHP micro-frameworks such as Slim and Silex.


3 Answers

Last update on 5th of March 2021

(This answer was getting a lot of attention from Laravel community so I thought of updating it.)

Laravel has officially stopped supporting sessions & views in laravel/lumen framework from version 5.2 and on wards.

But laravel still have a component illuminate/session which can be installed in lumen/framework and we can play around with this.

Step - 1

install illuminate/session using

composer require illuminate/session

Step - 2

Now goto bootstrap/app.php and add this middleware

$app->middleware([
    \Illuminate\Session\Middleware\StartSession::class,
]);

Purpose of adding the above middleware is to start session on every request and save session before serving response.

Step - 3

Now add config/session.php, since it is not present in Lumen by default. You can take session.php from Laravel official repo.

Step - 4

Create framework session storage directory by

mkdir -p storage/framework/sessions

Thanks to DayDream

Step - 5

In bootstrap/app.php add bindings for \Illuminate\Session\SessionManager

$app->singleton(Illuminate\Session\SessionManager::class, function () use ($app) {
    return $app->loadComponent('session', Illuminate\Session\SessionServiceProvider::class, 'session');
});

$app->singleton('session.store', function () use ($app) {
    return $app->loadComponent('session', Illuminate\Session\SessionServiceProvider::class, 'session.store');
});

Thanks to @xxRockOnxx for finding loadComponent method. It takes 3 arguments,

  • first one is config file name. (file should be present in config/ directory)
  • second is ServiceProvider FQN
  • third is return of this method.

loadComponent just calls the $app->register and inject $app while building the ServiceProvider

How to Use


// Save Session
$router->get('/', function (\Illuminate\Http\Request $request) {

    $request->session()->put('name', 'Lumen-Session');

    return response()->json([
        'session.name' => $request->session()->get('name')
    ]);
});


// Test session
$router->get('/session', function (\Illuminate\Http\Request $request) {

    return response()->json([
        'session.name' => $request->session()->get('name'),
    ]);
});

I've also added example over github supporting from lumen framework v5.6 to all the way to current version v8.0.

https://github.com/rummykhan/lumen-session-example

like image 84
rummykhan Avatar answered Oct 14 '22 01:10

rummykhan


It is important to that you also use $request->session(), otherwise it will not work.

like image 6
Kevin Upton Avatar answered Oct 14 '22 01:10

Kevin Upton


I tried the solution mentioned above, however, it's also required to create a folder storage/framework/sessions if using the default settings.

like image 4
Daydream Avatar answered Oct 14 '22 02:10

Daydream