dwendrich/expressive-session-middleware
Composer 安装命令:
composer require dwendrich/expressive-session-middleware
包简介
Session handling middleware for use with zend expressive 3 based on zend-session.
README 文档
README
Session handling middleware based on zend-session for use in zend expressive 3 applications.
PSR-15 Support
This version supports PSR-15 instead of http-interop/http-middleware interfaces, as currently implemented by zend expressive 3. For use with older versions of zend expressive, please refer to version 0.1.9.
Requirements
- PHP 7.1 or above
- zendframework/zend-session
Installation
Install the latest version with composer. For information on how to get composer or how to use it, please refer to getcomposer.org.
$ composer require dwendrich/expressive-session-middleware
If during installation you are prompted to inject Zend\Session\ConfigProvider into your configuration, you can simply
ignore and continue without it. All relevant configuration is part of SessionMiddleware\ConfigProvider.
As part of a zend-expressive application add SessionMiddleware\ConfigProvider::class to config/config.php:
$aggregator = new ConfigAggregator([ // enable SessionMiddleware SessionMiddleware\ConfigProvider::class, // ... other stuff goes here // Load application config in a pre-defined order in such a way that local settings // overwrite global settings. (Loaded as first to last): // - `global.php` // - `*.global.php` // - `local.php` // - `*.local.php` new PhpFileProvider('config/autoload/{{,*.}global,{,*.}local}.php'), // Load development config if it exists new PhpFileProvider('config/development.config.php'), ], $cacheConfig['config_cache_path']);
There are two ways of integrating the session middleware into your application.
1. Add the middleware to the programmatic middlewarepipeline
You can add the middleware to the file config/pipeline.php:
// Register session handling middleware $app->pipe(SessionMiddleware::class); // Register the routing middleware in the middleware pipeline $app->pipe(\Zend\Expressive\Router\Middleware\RouteMiddleware::class); $app->pipe(ImplicitHeadMiddleware::class); $app->pipe(ImplicitOptionsMiddleware::class); $app->pipe(UrlHelperMiddleware::class);
Depending on which middleware should get access to the session, you should prepend SessionMiddleware in the pipeline.
Commonly before registering the routing middleware is a good way to go.
This way the middleware is invoked on every request to your application. Since session handling may produce some overhead, which isn't always needed, there is an alternative:
2. Add the middleware to a specific route
Add a route definition to either config/routes.php or a RouteDelegator as part of your application:
$app->route( '/path-to-my-action', [ SessionMiddleware::class, MyApp\Action\MyAction::class ], ['GET'], 'path-to-my-action' );
This way session handling is bound to a specific path in your application where it may be needed.
For further information on programmatic pipelines and routing in zend expressive please refer to the documentation.
Basic usage
Once the session middleware is invoked it will start the session and adds the session manager object as attribute to the current request. Any middleware which processes this request subsequently, can detect that session handling is started by testing against the request attribute:
/** * Process an incoming server request and return a response, optionally delegating * to the next middleware component to create the response. * * @param ServerRequestInterface $request * @param DelegateInterface $delegate * * @return ResponseInterface */ public function process(ServerRequestInterface $request, DelegateInterface $delegate) : ResponseInterface { $sessionManager = $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE, false); if ($sessionManager) { // sessionManager is present and can be used } // further request processing goes here... }
Storing and retrieving session data
Zend session component uses Container objects to access and store session data. For information on this concept please
refer to the documentation.
Following is a simple example on how to use a Container:
use Zend\Session\Container; $container = Container('my_namespace'); // save 'foo' into the `item` key $container->item = 'foo';
In another part of the application you may want to access this data:
use Zend\Session\Container; $container = Container('my_namespace'); // read the content from the `item` key $foo = $container->item;
Configuration
The session can be configured by adding a session.global.php to your config/autoload path, for example. You can
use session.global.php.dist file (see session.global.php.dist) as template.
return [ 'session' => [ 'config' => [ 'options' => [ 'name' => 'my_special_session_name', 'use_cookies' => true, 'cookie_secure' => false, ], ], ], ];
For possible configuration options please refer to the documentation of zend-session component.
You can override the session configuration instance with any instance or class implementing
Zend\Session\Config\ConfigInterface. Simply specify it in session configuration:
return [ 'session' => [ 'class' => Zend\Session\Config\StandardConfig::class, 'options' => [ 'name' => 'my_app', ], ], ];
For using a certain session storage adapter you can override it in the config, as well. Therefore it has to implement
Zend\Session\Storage\StorageInterface:
return [ 'session' => [ 'storage' => new MyApp\Session\StorageAdapter::class, ], ];
To add validators to the session manager, you cann define them in the config, too, for example:
return [ 'session' => [ 'validators' => [ Session\Validator\RemoteAddr::class, Session\Validator\HttpUserAgent::class, ], ], ];
dwendrich/expressive-session-middleware 适用场景与选型建议
dwendrich/expressive-session-middleware 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.27k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2017 年 04 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「zend」 「middleware」 「expressive」 「session handling」 「psr-15」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dwendrich/expressive-session-middleware 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dwendrich/expressive-session-middleware 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dwendrich/expressive-session-middleware 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
LosLog provides some log utility
An Zend Expressive module for loading ZF2 or ZF3 modules.
Polyfill for mb_ereg(), mb_eregi(), mb_ereg_match(), and mb_ereg_replace*() functions; primary use case is for mbstring on Windows 7.4+
Zend Framework 1 Http package
Zend Framework 1 Soap package
统计信息
- 总下载量: 3.27k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-04-25