innmind/http-session

4.0.0 2023-10-22 14:31 UTC

This package is auto-updated.

Last update: 2024-10-22 16:31:17 UTC


README

Build Status codecov Type Coverage

Library to manage session for http requests.

The goal is to break the paradigm of considering the request and response as a global environment. Request and response should be delt as transiting data. The session for a request should obey this principle as well, thus the signature Manager::start(ServerRequest): Maybe<Session>.

Installation

composer require innmind/http-session

Usage

use Innmind\HttpSession\Manager\Native;
use Innmind\Http\{
    Message\Response\Response,
    Message\ServerRequest,
    Message\StatusCode,
    Headers,
    Header\SetCookie,
    Header\CookieParameter\HttpOnly,
    Header\CookieParameter\Domain,
    Header\Parameter\Parameter,
};

$manager = Native::of();
$request = /* an instance of ServerRequest */

$session = $manager->start($request)->match(
    static fn($session) => $session,
    static fn() => throw new \RuntimeException('Unable to start the exception'),
);
// inject some data in the session
$manager->save($session);

$response = new Response(
    $code = StatusCode::ok,
    $request->protocolVersion(),
    Headers::of(
        SetCookie::of(
            new Parameter($session->name()->toString(), $session->id()->toString()),
            new HttpOnly,
            new Domain($request->url()->authority()->host()),
        ),
    ),
);
// send the response

Note: you should take a look at innmint/http-server in order to know how to have access to an instance of ServerRequest and send the Response.