stefna/cookie
Composer 安装命令:
composer require stefna/cookie
包简介
Cookie management for psr7/psr15
README 文档
README
The package helps in working with HTTP cookies in a PSR-7/PSR-15 environment.
Requirements
PHP 8.2 or higher.
Installation
composer require stefna/cookie
Motivation
When using PSR-7 the setcookie() function becomes useless when you want
everything connected to the request-response loop to be in the Request and
Response objects.
setcookie() also have a tendency to not support the latest features of the
http cookie standard.
Concept
The main concept of package is the separation of read/write of cookie from
using the cookie so with that in mind the CookieJar is not connected to the
http layer it's just a container that stores cookies.
It's then up to the CookieMiddleware and CookieManager to read and write
the http headers.
By separating it like this it's easy to implement Signing or encryption of the cookies since it's not part of the code that uses the cookies.
It also means you don't need the Response object when creating new cookies all
you need to do is add the cookie to the CookieJar and the middleware deals
with writing the cookie
Usage
Usage of cookie jar from inside action.
The CookieJar is added to the request by the CookieMiddleware that
middleware also deals with adding the cookies to the response headers.
use Psr\Http\Message\ServerRequestInterface; class Action { public function __invoke(ServerRequestInterface $request) { $cookieJar = $request->getAttribute(\Stefna\Cookie\CookieJar::class); if (!$cookieJar->has('visits')) { $cookieJar->set(new \Stefna\Cookie\Cookie('visits', 0)); } $visitCookie = $cookieJar->get('visits'); if ($cookieJar->getInt('visits') < 10) { $cookieJar->set($visitCookie ->withValue($cookieJar->getInt('visits') + 1) ->withExpires(DateInterval::createFromDateString('+1 week')) ); } else { // remove cookie alt 1 $cookieJar->set($visitCookie->expire()); // remove cookie alt 2 $cookieJar->remove($visitCookie); } } }
Contribute
We are always happy to receive bug/security reports and bug/security fixes
License
The MIT License (MIT). Please see License File for more information.
stefna/cookie 适用场景与选型建议
stefna/cookie 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.21k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 01 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 stefna/cookie 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 stefna/cookie 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.21k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-01-16