Psr-15 middleware allowing to use session with Psr-7 request and response

0.3.0 2018-04-27 14:10 UTC

This package is auto-updated.

Last update: 2024-05-25 23:29:30 UTC


This package provides a Psr-15 middleware managing session. It works out of the box using the session configuration values, yet it is suited to the Psr-7 request/response abstraction.

Require php >= 7.0

Installation composer require ellipse/session

Run tests ./vendor/bin/kahlan

Using the session middleware


use Ellipse\Dispatcher;
use Ellipse\Session\SessionMiddleware;

// By default SessionMiddleware uses php session configuration values.
$middleware = new SessionMiddleware

    // Will call php session_set_save_handler($handler) before starting the session.

    // Will call php session_name('session_name') before starting the session.

    // Will call php session_save_path('/session/save/path') before starting the session.

    // Will call php session_cache_limiter('public') before starting the session.

    // Will call php session_cache_expire(60) before starting the session.

    // Will call php session_set_cookie_params(3600, '/path', 'domain', true, true) before starting the session.
        'lifetime' => 3600,
        'path' => '/path',
        'domain' => 'domain',
        'secure' => true,
        'httponly' => true,

// Build a dispatcher using the session middleware.
$dispatcher = new Dispatcher([


    // Next middleware have access to the request Ellipse\Session::class attribute.
    new class implements MiddlewareInterface
        public function process(ServerRequestInterface $request, RequestHandlerInterface $handler)
            // Session data is attached to the Ellipse\Session::class request attribute.
            $session = $request->getAttribute(Ellipse\Session::class);

            // Return the session id (session_id())

            // Regenerate the session id (session_regenerate_id(bool $delete_old_session = false))

            // Set a session value.
            $session->set('key', 'value');

            // Set a session value only for the next session.
            $session->flash('key', 'value');

            // Return whether a session value is set.

            // Return an array of all the session data.

            // Return the value associated to the given key.

            // Return a default value when the given key is not set.
            $session->get('notset', 'default');

            // Unset a session value.

            // Unset all session value.