innmind / http-session
HTTP session
4.0.0
2023-10-22 14:31 UTC
Requires
- php: ~8.2
- innmind/http: ~7.0
- innmind/immutable: ~4.9|~5.0
Requires (Dev)
- innmind/coding-standard: ~2.0
- phpunit/phpunit: ~9.0
- vimeo/psalm: ~5.12
This package is auto-updated.
Last update: 2024-10-22 16:31:17 UTC
README
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
.