yiisoft/session
Composer 安装命令:
composer require yiisoft/session
包简介
A session service, PSR-15 session middleware, and a flash message service which helps use one-time messages.
关键字:
README 文档
README
Yii Session
The package implements a session service, PSR-15 session middleware, and a flash message service which helps use one-time messages.
Requirements
- PHP 8.0 - 8.5.
Installation
The package could be installed with Composer:
composer require yiisoft/session
In order to maintain a session between requests you need to add SessionMiddleware to your route group or
application middlewares. Route group should be preferred when you have both API with token-based authentication
and regular web routes in the same application. Having it this way avoids starting the session for API endpoints.
Yii 3 configuration
In order to add a session for a certain group of routes, edit config/routes.php like the following:
use Yiisoft\Router\Group; use Yiisoft\Session\SessionMiddleware; return [ Group::create('/blog') ->middleware(SessionMiddleware::class) ->routes( // ... ) ];
To add a session to the whole application, edit config/application.php like the following:
return [ Yiisoft\Yii\Http\Application::class => [ '__construct()' => [ 'dispatcher' => DynamicReference::to(static function (Injector $injector) { return ($injector->make(MiddlewareDispatcher::class)) ->withMiddlewares( [ ErrorCatcher::class, SessionMiddleware::class, // <-- add this CsrfMiddleware::class, Router::class, ] ); }), ], ], ];
General usage
You can access session data through SessionInterface.
public function actionProfile(\Yiisoft\Session\SessionInterface $session) { // get a value $lastAccessTime = $session->get('lastAccessTime'); // get all values $sessionData = $session->all(); // set a value $session->set('lastAccessTime', time()); // check if value exists if ($session->has('lastAccessTime')) { // ... } // remove value $session->remove('lastAccessTime'); // get value and then remove it $sessionData = $session->pull('lastAccessTime'); // clear session data from runtime $session->clear(); }
In case you need some data to remain in session until read, such as in case with displaying a message on the next page flash messages is what you need. A flash message is a special type of data, that is available only in the current request and the next request. After that, it will be deleted automatically.
FlashInteface usage is the following:
/** @var Yiisoft\Session\Flash\FlashInterface $flash */ // request 1 $flash->set('warning', 'Oh no, not again.'); // request 2 $warning = $flash->get('warning'); if ($warning !== null) { // do something with it }
Opening and closing session
public function actionProfile(\Yiisoft\Session\SessionInterface $session) { // start session if it's not yet started $session->open(); // work with session // write session values and then close it $session->close(); }
Note: Closing session as early as possible is a good practice since many session implementations are blocking other requests while session is open.
There are two more ways to close session:
public function actionProfile(\Yiisoft\Session\SessionInterface $session) { // discard changes and close session $session->discard(); // destroy session completely $session->destroy(); }
Custom session storage
When using Yiisoft\Session\Session as session component, you can provide your own storage implementation:
$handler = new MySessionHandler(); $session = new \Yiisoft\Session\Session([], $handler);
Custom storage must implement \SessionHandlerInterface.
Documentation
If you need help or have a question, the Yii Forum is a good place for that. You may also check out other Yii Community Resources.
License
The Yii Session is free software. It is released under the terms of the BSD License.
Please see LICENSE for more information.
Maintained by Yii Software.
Support the project
Follow updates
yiisoft/session 适用场景与选型建议
yiisoft/session 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 338.54k 次下载、GitHub Stars 达 20, 最近一次更新时间为 2020 年 09 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「middleware」 「session」 「flash」 「psr-15」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yiisoft/session 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yiisoft/session 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yiisoft/session 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Secure session middleware for Slim 3 framework
Non-I/O blocking PHP SessionHandler implementation using Nette/Caching with DI Extension for Nette framework
Slim Framework 3 CSRF protection middleware utilities
Cloudflare Middleware For Guzzle
Magento - Clean up session extension
统计信息
- 总下载量: 338.54k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 20
- 点击次数: 19
- 依赖项目数: 37
- 推荐数: 2
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2020-09-11