在 Zend Framework 2 中启动和运行会话的最佳方法是什么?我尝试过设置session_start()
在我的 index.php 文件中,但随后在引导任何自动加载器之前运行该文件,导致我的会话中存在不完整的对象。
在 ZF1 中,您可以通过在配置中添加一些选项来初始化会话,但我不知道如何在 ZF2 中执行此操作。
如果我理解正确的话,您想要做的就是让您的会话在您的模块中正常工作?假设这是正确的,有两个步骤。
1)创建配置:模块.config.php
return array(
'session' => array(
'remember_me_seconds' => 2419200,
'use_cookies' => true,
'cookie_httponly' => true,
),
);
2) 开始您的会话:模块.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,请参阅这个问题。感谢 Andreas Linden 的最初回答 - 我只是复制粘贴他的答案。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)