在 Zend Framework 2 中启动和运行 session 的最佳方法是什么?我试过在我的 index.php 文件中设置 session_start()
,但是在引导任何自动加载器之前它就运行了,导致我的 session 中的对象不完整。
在 ZF1 中,您可以通过在配置中添加一些选项来初始化 session ,但我不知道如何在 ZF2 中执行此操作。
最佳答案
如果我没理解错的话,你想做的就是让你的 session 在你的模块中正常工作吗?假设这是正确的,有两个步骤。
1) 创建配置:module.config.php
return array(
'session' => array(
'remember_me_seconds' => 2419200,
'use_cookies' => true,
'cookie_httponly' => true,
),
);
2) 开始你的 session :Module.php
use Zend\Session\Config\SessionConfig;
use Zend\Session\SessionManager;
use Zend\Session\Container;
use Zend\EventManager\EventInterface;
public function onBootstrap(EventInterface $evm)
{
$config = $evm->getApplication()
->getServiceManager()
->get('Configuration');
$sessionConfig = new SessionConfig();
$sessionConfig->setOptions($config['session']);
$sessionManager = new SessionManager($sessionConfig);
$sessionManager->start();
/**
* Optional: If you later want to use namespaces, you can already store the
* Manager in the shared (static) Container (=namespace) field
*/
Container::setDefaultManager($sessionManager);
}
在 \Zend\Session\Config\SessionConfig 的文档中找到更多选项
如果你也想存储cookie,那么请看this Question .感谢 Andreas Linden 的初步回答 - 我只是复制粘贴他的内容。
关于php - 如何在 Zend Framework 2 中引导 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12774809/