locosoftworks/zf2-cookie-session

用于将会话存储在cookie中的会话保存处理程序

dev-master 2014-07-10 00:29 UTC

This package is not auto-updated.

Last update: 2024-09-28 16:03:10 UTC


README

要求

Composer 安装

composer.json 中添加以下内容并执行 composer update

"minimum-stability": "dev",
"require": {
    "locosoftworks/zf2-cookie-session": "dev-master"
}

用法

要在 ZF2 中使用会话处理程序,您需要确保在返回任何输出之前写入会话。这可以通过向 SendResponseEvent::EVENT_SEND_RESPONSE 事件添加监听器或使用提供的 SessionManager 来实现。这可以在您的 application.config.php 中完成。

'service_manager' => array(
	'invokables' => array(
	    'SessionManager' => 'Loco\Session\SessionManager',
	),
)

要加密客户端会话,您需要提供自己的加密方法,该方法实现 \Loco\Crypt\CipherInterface。有关如何使用提供的加密方法的更多信息,请参阅ZF2 块加密文档

$cipher = new \Loco\Crypt\BlockCipher(new \Zend\Crypt\Symmetric\Mcrypt(array('algo' => 'aes')));
$cipher->setKey('This is my application secret key');

这需要在您的堆栈中足够高,以便在尝试启动会话之前调用。代码的位置可能会有所不同,但将代码放在 onBootstrap 方法的顶部应该足够。

// assuming $sm is your ServiceManager and SessionManager is a Loco\Session\SessionManager
$sessionManager = $sm->get('SessionManager');

// ClientSessionAdapter is a ZF2 wrapper to the ClientSession
$sessionAdapter = new \Loco\Session\SaveHandler\ClientSessionAdapter();

// set the cipher
$sessionAdapter->setCipher($cipher);

$sessionManager->setSaveHandler($sessionAdapter);

// the SessionManager NEEDS the EventManager to set up the necessary listeners
$sessionManager->setEventManager($eventManager);

$sessionManager->start();