如何在Zend Framework 2中引导会话 [英] How to Bootstrap Sessions in Zend Framework 2

查看:52
本文介绍了如何在Zend Framework 2中引导会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Zend Framework 2中启动和运行会话的最佳方法是什么?我曾尝试在index.php文件中设置session_start(),但是在启动任何自动加载器之前会先运行它,导致会话中的对象不完整.

What is the best way to go about getting sessions up and running in Zend Framework 2? I've tried setting session_start() in my index.php file but then that gets run before any autoloaders have been bootstrapped, leading to incomplete objects sitting in my sessions.

在ZF1中,您可以通过在配置中添加一些选项来初始化会话,但是我对在ZF2中执行此操作感到困惑.

In ZF1 you could have it initialize sessions by adding some options into the config, but I'm at a loss on how to do this in ZF2.

推荐答案

如果我正确理解您的意思,那么您想做的就是让您的会话在您的模块中正常工作?假设这是正确的,那么只需两个步骤.

If i understand you correctly, all you wanna do is have your session working properly in your modules? Assuming that's correct there are two single steps.

1)创建配置: module.config.php

return array(
    'session' => array(
        'remember_me_seconds' => 2419200,
        'use_cookies' => true,
        'cookie_httponly' => true,
    ),
);

2)开始您的会话: Module.php

2) Start your 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,请参阅此问题.感谢Andreas Linden的最初回答-我只是复制粘贴他.

If you want to store cookies too, then please see this Question. Credits to Andreas Linden for his initial answer - I'm simply copy pasting his.

这篇关于如何在Zend Framework 2中引导会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆