dwendrich/expressive-session-middleware

基于zend-session的用于zend expressive 3的会话处理中间件。

1.0.0 2018-06-21 09:38 UTC

This package is auto-updated.

Last update: 2024-09-29 04:37:06 UTC


README

基于zend-session的会话处理中间件,用于zend expressive 3应用程序。

Build Status Coverage Status Latest Stable Version

PSR-15支持

本版本支持PSR-15而不是http-interop/http-middleware接口,后者目前由zend expressive 3实现。对于使用较旧版本的zend expressive,请参阅版本0.1.9。

要求

安装

使用composer安装最新版本。有关如何获取composer或如何使用它的信息,请参阅getcomposer.org

$ composer require dwendrich/expressive-session-middleware

如果在安装过程中提示您将Zend\Session\ConfigProvider注入到配置中,您可以简单地忽略并继续,而不使用它。所有相关配置都是SessionMiddleware\ConfigProvider的一部分。

作为zend-expressive应用程序的一部分,将SessionMiddleware\ConfigProvider::class添加到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']);

将会话中间件集成到应用程序中的两种方法。

1. 将中间件添加到程序性中间件管道

您可以将中间件添加到文件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);

根据哪个中间件需要访问会话,您应该在管道中先添加SessionMiddleware。通常在注册路由中间件之前是一个不错的选择。

这种方式下,中间件会在每次对应用程序的请求上被调用。由于会话处理可能会产生一些开销,这并不总是需要的,所以有另一种方式

2. 将中间件添加到特定路由

将路由定义添加到config/routes.php或作为应用程序的一部分的RouteDelegator

$app->route(
    '/path-to-my-action',
    [
        SessionMiddleware::class,
        MyApp\Action\MyAction::class
    ],
    ['GET'],
    'path-to-my-action'
);

这种方式下,会话处理被绑定到应用程序中可能需要会话的具体路径。

有关zend expressive中的程序性管道和路由的更多信息,请参阅文档

基本用法

一旦会话中间件被调用,它将开始会话并将会话管理器对象作为属性添加到当前请求中。任何随后处理此请求的中间件都可以通过检查请求属性来检测会话处理是否开始

/**
 * 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...
}

存储和检索会话数据

Zend会话组件使用Container对象来访问和存储会话数据。有关此概念的信息,请参阅文档

以下是一个如何使用Container的简单示例

use Zend\Session\Container;
 
$container = Container('my_namespace');
 
// save 'foo' into the `item` key
$container->item = 'foo';

在应用程序的另一部分,您可能想访问这些数据

use Zend\Session\Container;
 
$container = Container('my_namespace');
 
// read the content from the `item` key
$foo = $container->item;

配置

会话可以通过将session.global.php添加到您的config/autoload路径来配置,例如。您可以使用session.global.php.dist文件(见session.global.php.dist)作为模板。

return [
    'session' => [
        'config' => [
            'options' => [
                'name' => 'my_special_session_name',
                'use_cookies' => true,
                'cookie_secure' => false,
            ],
        ],
    ],
];

有关可能的配置选项,请参阅zend-session组件的文档。

您可以使用任何实现 Zend\Session\Config\ConfigInterface 的实例或类来覆盖会话配置实例。只需在会话配置中指定即可。

return [
    'session' => [
        'class' => Zend\Session\Config\StandardConfig::class,
        'options' => [
            'name' => 'my_app',
        ],
    ],
];

若要使用特定的会话存储适配器,您也可以在配置中覆盖它。因此,它必须实现 Zend\Session\Storage\StorageInterface

return [
    'session' => [
        'storage' => new MyApp\Session\StorageAdapter::class,
    ],
];

要向会话管理器添加验证器,您也可以在配置中定义它们,例如。

return [
    'session' => [
        'validators' => [
            Session\Validator\RemoteAddr::class,
            Session\Validator\HttpUserAgent::class,
        ],
    ],
];