innmind / http-session
HTTP会话
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-09-22 16:15:37 UTC
README
管理HTTP请求会话的库。
目标是打破将请求和响应视为全局环境的范式。请求和响应应被视为传输数据。请求的会话也应遵守此原则,因此签名 Manager::start(ServerRequest): Maybe<Session>
。
安装
composer require innmind/http-session
用法
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
注意:您应该查看 innmint/http-server
以了解如何访问 ServerRequest
实例并发送 Response
。