odan / session

A middleware oriented session handler for PHP and Slim 4+
https://odan.github.io/session/
MIT License
58 stars 11 forks source link

SessionStartMiddleware not starting session #35

Closed AlexandreGagner closed 4 months ago

AlexandreGagner commented 4 months ago

Hello, I updated my slim 4 base with the v6 ans followed all v6 doc/integration.

I feel i missed something because the session are not starting when i register the SessionStartMiddleware :

 <?php

use Odan\Session\Middleware\SessionStartMiddleware;
use App\Middleware\SentryMiddleware;
use App\Middleware\AuthMiddleware;
use Slim\App;
use Slim\Middleware\ErrorMiddleware;
use Slim\Views\TwigMiddleware;

return function (App $app) {
    $app->add(SessionStartMiddleware::class);
    $app->add(SentryMiddleware::class);
    $app->add(AuthMiddleware::class);
    $app->addBodyParsingMiddleware();
    $app->add(TwigMiddleware::class);
    $app->addRoutingMiddleware();
    $app->add(new Zeuxisoo\Whoops\Slim\WhoopsMiddleware());
}; 

I have to start it the container definition (i believe DI container should not start a session automatically as mentioned in v6 doc). I'm missing somethings ?

Thanks 😅

odan commented 4 months ago

Slim processes middleware in a Last In, First Out (LIFO) order. This means the last middleware added is the first one to be executed. If you add multiple middleware components, they will be executed in the reverse order of their addition. In your case, SessionStartMiddleware will be executed at the end, but some other middleware may need a started session.

Try to move the $app->add(SessionStartMiddleware::class); call down to execute it earlier.

Example:

return function (App $app) {
    $app->add(SentryMiddleware::class);
    $app->add(AuthMiddleware::class);
    $app->addBodyParsingMiddleware();
    $app->add(TwigMiddleware::class);
    $app->add(SessionStartMiddleware::class); // <--- new position
    $app->addRoutingMiddleware();
    $app->add(new Zeuxisoo\Whoops\Slim\WhoopsMiddleware());
}; 
AlexandreGagner commented 4 months ago

Thanks it's working ! Completely forgot about LIFO in slim 4.

Regards