php - session 容器在 zf2 中不工作?

标签 php session zend-framework2

我在 StackOverflow 上看到很多问题,我有这段代码

use Zend\Session\Container;

class IndexController extends AbstractActionController {

public function indexAction() {

     $userSession = new Container('user');
     $userSession->username = 'Sandhya';
     return new ViewModel();
     }
}

当我在 Controller 中打印 $userSession 容器时,它会给我这个输出

Zend\Session\Container Object ( 
    [name:protected] => user 
    [manager:protected] => Zend\Session\SessionManager Object (
        [defaultDestroyOptions:protected] => Array ( 
            [send_expire_cookie] => 1 
            [clear_storage] => 
        ) 
        [name:protected] => 
        [validatorChain:protected] => 
        [config:protected] => Zend\Session\Config\SessionConfig Object (
            [phpErrorCode:protected] => 
            [phpErrorMessage:protected] => 
            [rememberMeSeconds:protected] => 240 
            [serializeHandler:protected] => 
            [validCacheLimiters:protected] => Array (
                [0] => nocache 
                [1] => public 
                [2] => private 
                [3] => private_no_expire 
            ) 
            [validHashBitsPerCharacters:protected] => Array ( 
                [0] => 4 
                [1] => 5 
                [2] => 6 
            ) 
            [validHashFunctions:protected] => 
            [name:protected] => 
            [savePath:protected] => 
            [cookieLifetime:protected] => 2592000 
            [cookiePath:protected] => 
            [cookieDomain:protected] => 
            [cookieSecure:protected] => 
            [cookieHttpOnly:protected] => 1 
            [useCookies:protected] => 1 
            [options:protected] => Array ( 
                [gc_maxlifetime] => 2592000 
            ) 
        ) 
        [defaultConfigClass:protected] => Zend\Session\Config\SessionConfig     
        [storage:protected] => Zend\Session\Storage\SessionArrayStorage Object (
        ) 
        [defaultStorageClass:protected] => Zend\Session\Storage\SessionArrayStorage 
        [saveHandler:protected] => 
    ) 
    [storage:protected] => Array ( ) 
    [flag:protected] => 2 
    [iteratorClass:protected] => ArrayIterator 
    [protectedProperties:protected] => Array ( 
        [0] => name 
        [1] => manager 
        [2] => storage 
        [3] => flag 
        [4] => iteratorClass 
        [5] => protectedProperties 
    ) 
)

这意味着没有什么像username...

但是当我打印 S_SESSION 时,它给了我这个输出...

Array ( 
    [__ZF] => Array ( 
        [_REQUEST_ACCESS_TIME] => 1429081041.81 
    ) 
    [user] => Zend\Stdlib\ArrayObject Object ( 
        [storage:protected] => Array ( 
            [username] => Sandhya 
        ) 
        [flag:protected] => 2 
        [iteratorClass:protected] => ArrayIterator     
        [protectedProperties:protected] => Array ( 
            [0] => storage 
            [1] => flag 
            [2] => iteratorClass 
            [3] => protectedProperties 
        ) 
    ) 
)

有一个字段username...

但是当我试图在 View 中获取 $_SESSION 时,它会给我与上面相同的输出..

问题是我无法在容器和 $_SESSION 中获取 username。 我需要它在 Controller 中。 什么问题需要帮助?谢谢。

最佳答案

我认为您必须处理您的配置。 您必须设置一个通用的 SessionManager 来管理 session 信息的处理。

像这样:

$sessionConfig = new SessionConfig();
$sessionConfig->setOptions($config);
$sessionManager = new SessionManager($sessionConfig);
$sessionManager->start();
Container::setDefaultManager($sessionManager);

我建议在您的 ServiceManager 实例中注册您的 SessionManager 配置,然后在整个应用程序中使用它。

'service_manager' => array(
    'factories' => array(
        'session_manager' => 'My\Factory\SessionManagerFactory'
    )
)

然后您可以在任何 Controller 中获取您的 SessionManager:

$sessionManager = $this->serviceLocator->get('session_manager');

如果您创建一个新的 Container,它将自动使用您的通用/默认 SessionManager 实例,因此所有这些都将在一个地方进行管理。

$userSession = new Container('user');
$userSession->getManager() === $this->serviceLocator->get('session_manager') // true

关于如何注册你的session_manager我会引用the official ZF2 documentation .

关于php - session 容器在 zf2 中不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29643600/

相关文章:

android - 每次点击而不是每次 session 的 Google Analytics 自定义变量

php - 实体内部的 DDD 和 Doctrine 映射

php - Zend Framework 和 Doctrine 2 - 我的单元测试是否足够?

php - 在 call_user_func_array(...) 中传递关联数组

PHP exec() 作为后台进程(Windows Wampserver 环境)

java - Hibernate 的 Session.close() 是否自动回滚未提交的事务?

java - 在ServletContextListener中访问 session 变量

php - Zend Framework 2 定时任务

php - 主干 - 我们的模型如何知道服务器是否使用 PHP 发送 "Error Message"

php - 将 cakephp 2.6 升级到 2.8 以使用 php 7