httpsoft / http-cookie
支持PSR-7的cookie管理
1.1.0
2023-05-05 20:37 UTC
Requires
- php: ^7.4|^8.0
- psr/http-message: ^1.1|^2.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- httpsoft/http-message: ^1.1
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.7
- vimeo/psalm: ^4.9|^5.2
Provides
README
本包根据RFC 6265规范提供便捷的cookie管理。
文档
安装
此包需要PHP版本7.4或更高。
composer require httpsoft/http-cookie
使用
use HttpSoft\Cookie\Cookie; use HttpSoft\Cookie\CookieCreator; use HttpSoft\Cookie\CookieManager; use HttpSoft\Cookie\CookieSendMiddleware; /** * @var Psr\Http\Message\ResponseInterface $response * @var Psr\Http\Message\ServerRequestInterface $request * @var Psr\Http\Server\RequestHandlerInterface $handler */ $manager = new CookieManager(); // Create cookie $cookie1 = new Cookie('test', 'value', '+1 hour'); // or $cookie2 = CookieCreator::create('test2', 'value', time() + 3600, '.example.com', '/path'); // or from raw `Set-Cookie` header $cookie3 = CookieCreator::createFromString('name=value; Path=/; Secure; HttpOnly; SameSite=Lax; ...'); // Set cookies to the manager $manager->set($cookie1); $manager->set($cookie2); $manager->set($cookie3); // Set all cookie to the response for sending $response = $manager->send($response); // or use `CookieSendMiddleware` middleware $middleware = new CookieSendMiddleware($manager); $response = $middleware->process($request, $handler); // Emit a response to the client // ...