[Solved] Slim & PSR-7 Storage-less HTTP Sessions


#1

Hi

I try to use the middleware PSR-7 Storage-less HTTP Sessions (4.0.0) with Slim 3.10…

I created a project with the slim/skeleton, adding the middleware with composer

composer require psr7-sessions/storageless

Next step, adding some settings in settings.php to specify key and session lifetime…

Finally, i added the folliowing line in middlewares.php

$app->add(\PSR7Sessions\Storageless\Http\SessionMiddleware::fromSymmetricKeyDefaults($container->get('settings')['session']['key'],$container->get('settings')['session']['sessionLifetime']));

… I received from slim :

 Slim Application Error
The application could not run because of the following error:

Details
Type: RuntimeException
Message: {} is not resolvable
File: /home/eric/slim-lpdp/vendor/slim/slim/Slim/CallableResolver.php
Line: 104
Trace
#0 /home/eric/slim-lpdp/vendor/slim/slim/Slim/CallableResolver.php(56): Slim\CallableResolver->assertCallable(Object(PSR7Sessions\Storageless\Http\SessionMiddleware))
#1 /home/eric/slim-lpdp/vendor/slim/slim/Slim/CallableResolverAwareTrait.php(45): Slim\CallableResolver->resolve(Object(PSR7Sessions\Storageless\Http\SessionMiddleware))
#2 /home/eric/slim-lpdp/vendor/slim/slim/Slim/DeferredCallable.php(36): Slim\DeferredCallable->resolveCallable(Object(PSR7Sessions\Storageless\Http\SessionMiddleware))
#3 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#4 /home/eric/slim-lpdp/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#5 /home/eric/slim-lpdp/vendor/slim/slim/Slim/MiddlewareAwareTrait.php(117): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))
#6 /home/eric/slim-lpdp/vendor/slim/slim/Slim/App.php(406): Slim\App->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#7 /home/eric/slim-lpdp/vendor/slim/slim/Slim/App.php(314): Slim\App->process(Object(Slim\Http\Request), Object(Slim\Http\Response))
#8 /home/eric/slim-lpdp/public/index.php(30): Slim\App->run()
#9 {main}

Where I’m wrong ? Could you help ?

Thanks for your support

Eric


#2

It looks like the PSR7Sessions\Storageless\Http\SessionMiddleware class is not a real “callable”.

Maybe the magic __invoke() method is just missing or so. Have you tried to port this example to slim?


#3

Thanks, i will try as soon as possible and i’ll give you a feedback


#4

Finally, I use two classes to adapt the PSR-15 interface to the slim middleware pattern

first, i inject the “Storage-less” class in the dependencies container:

$container['session'] = \PSR7Sessions\Storageless\Http\SessionMiddleware::fromSymmetricKeyDefaults($container->get('settings')['session']['key'],$container->get('settings')['session']['sessionLifetime']);

second i define two classes:

class SessionMiddleware extends BaseMiddleware
{
    public function __invoke(ServerRequestInterface $req, ResponseInterface $res, callable $next): ResponseInterface
    {
        ;
        /**
         * @var \PSR7Sessions\Storageless\Http\SessionMiddleware
         */
        $sessionMiddleware = $this->di->get('session');
        $enc = new encapsulated($res, $next);
        return $sessionMiddleware->process($req, $enc);

    }
}

class encapsulated implements RequestHandlerInterface
{
    private $next;
    private $response;

    /**
     * encapsulated constructor.
     * @param ResponseInterface $response
     * @param callable $next
     */
    public function __construct(ResponseInterface $response, callable $next)
    {
        $this->next = $next;
        $this->response = $response;
    }

    /**
     * Handle the request and return a response.
     * @param ServerRequestInterface $request
     * @return ResponseInterface
     */
    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        return call_user_func($this->next, $request, $this->response);
    }
}

Third and last thing, i add the middleware to the slim app:

$app->add(new SessionMiddleware($container));

Seems working fine.

Thanks for your help