session-interop / middleware.async
此包包含一个zend的中间件,可以将Session对象注入到请求中
dev-master
2017-07-21 17:40 UTC
Requires
- php: ^7.1
- container-interop/container-interop: ^1.0
- container-interop/service-provider: ~0.3.0
- psr/http-message: ^1.0
- session-interop/session-factory-interop: ^1.0
- session-interop/session-interop: ^3.0
- session-interop/session-manager-interop: ^1.0
- session-interop/utils.arraysession.savable: ^1.0
- session-interop/utils.defaultmanager: ^1.0
- thecodingmachine/middleware-list-universal-module: ~1.0
This package is not auto-updated.
Last update: 2024-09-14 19:53:17 UTC
README
此中间件将一个 SessionInterface
作为psr7请求的一个 属性 注入。属性的名称定义为 Introp\Session\SessionInterface::class。它 不会 在存在 $_SESSION 的情况下重新配置会话。此中间件分为两步执行
- 如果需要,此中间件会打开会话(通过调用
session_start
),然后创建一个扩展了ArraySession
的实例并将其复制,然后立即关闭它(通过调用session_abort
),以保留会话状态。一旦会话被复制,它将被注入到PSR7请求中,然后调用后续的中间件。 - 一旦执行了每个后续的中间件,对象上的新/删除/修改的会话值将被写入 $_SESSION,然后会话被持久化。再次强调,它将根据前一个会话的状态重新打开会话或允许它关闭。
注意:持久化意味着手动调用 session_write_close
,这将意味着所有 $_SESSION 都将被写入。
推荐
我们建议尽早将此中间件放置在您的应用程序的管道中,这样后续的中间件就可以使用请求的会话。
必需
此中间件被设计为 zend-stratigility 中间件使用。
使用方法
如果您想更改配置,您必须提供 SessionConfigurationInterface
实现作为工厂参数。当前服务器的配置是通过使用包 DefaultSessionConfiguration
来使用的
以下示例假设您使用
zend-expressive
和一个与- A
container-interop
兼容的依赖容器
###基本使用
// Without container $mySessionConfiguration = null; // If you have custom configuration: $mySessionConfiguration = new MySessionConfiguration(); $sessionMiddleware = \Interop\Session\Middleware\Async\AsyncSessionMiddlewareFactory::createFromConfiguration($mySessionConfiguration); // $mySessionConfiguration is optional $app->pipe($sessionMiddleware); // OR using container $factory = new \Interop\Session\Middleware\Async\AsyncSessionMiddlewareFactory(); $container->set(Interop\Session\Middleware\Async\AsyncSessionMiddlewareServiceProvider::class, $factory->__invoke($container)); //.... $app->pipe( $container->get(\Interop\Session\Middleware\Async\AsyncSessionMiddlewareServiceProvider::class) );
现在在后续的每个中间件中
$session = $request->getAttribute(\Interop\Session\SessionInterface::class); $session->set("foo", "baz"); //... echo $session->get("foo"); // print baz
使用服务提供者
service-provider
. 以下示例使用 Simplex
。如果提供者找到了 SessionConfigurationInterface
的实例,它将被使用。为了使容器能够注入该实例,它必须使用名称 Interop\Session\Configuration\SessionConfigurationInterface
。此名称是通过
use Interop\Session\Configuration\SessionConfigurationInterface; //..... SessionConfigurationInterface::class
$container->register( new \Interop\Session\Middleware\Async\AsyncSessionMiddlewareServiceProvider() ); /// ............. $app->pipe( $container->get(\Interop\Session\Middleware\Async\AsyncSessionMiddlewareServiceProvider::class) )
现在在后续的每个中间件中
$session = $request->getAttribute(\Interop\Session\SessionInterface::class); $session->set("foo", "baz"); echo $session->get("foo"); // print baz
使用 Aura.DI
$factory = "\\Interop\\Session\\Middleware\\Async\\AsyncSessionMiddlewareFactory"; $name = "\\Interop\\Session\\Middleware\\Async\\AsyncSessionMiddleware"; $container->set($factory, $container->lazyNew($factory)); $container->set($name, $container->lazyGetCall($factory, '__invoke', $container)); /// ....... $app->pipe($container->get("\\Interop\\Session\\Middleware\\Async\\AsyncSessionMiddleware"));