【问题标题】:Session container not working in zf2?会话容器在 zf2 中不起作用?
【发布时间】:2015-06-21 00:32:48
【问题描述】:

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

use Zend\Session\Container;

class IndexController extends AbstractActionController {

public function indexAction() {

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

当我在控制器中打印$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...

但是当我尝试查看 $_SESSION 时,它给了我与上面相同的输出..

问题是我无法在容器和$_SESSION 中获得username。 我需要它在控制器中。 可能是什么问题需要帮助?谢谢。

【问题讨论】:

  • 您希望此用户名出现在视图部分吗?
  • 不,我想在控制器中使用这个
  • 好的,我正在发布对您有帮助的答案。
  • 会话容器只是$_SESSION 的包装器。 var_dump 容器时看不到您的用户名,因为它存储在会话中。如果您无法通过以下方式访问用户名。视图中的容器,请编辑您的问题以包含您用于测试的代码。
  • 我正在控制器本身中获取会话的值并将这些值传递给查看 ....

标签: php session zend-framework2


【解决方案1】:

您可以使用以下代码:

$userSession = new Container('user');
//To check the session variable in zf2:
if($userSession->offsetExists('username')){
   //Your logic after check condition
}

这将根据会话是否存在返回真或假。

//To get the value of session:
echo $user->offsetGet('username');

以上代码将返回会话索引username的值。

您可以使用以下代码代替$userSession->username = 'Sandhya';

$user->offsetSet('username','Sandhya');

这是zf2标准,zf2中的会话容器使用。

【讨论】:

  • echo $userSession->offsetExists('username');在其他控制器中什么都不做。
  • @SandhyaGor 首先使用$userSession = new Container('user'); 然后回显它。
  • 安装有问题吗?
  • 因为一切正常,但只有会话。
  • 在这样的条件下尝试:if (! ($user->offsetExists('username'))) { echo "Session exit"; } 还要检查您的会话是否已创建。
【解决方案2】:

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

类似这样的:

$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'
    )
)

然后您可以在任何控制器中获取您的 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

【讨论】:

  • 我已经设置了这个配置,但没有创建 ServiceManager 实例,因为它很好,但问题仍然存在。我已经安装了另一个 Zend 包,容器运行良好。但现在我必须将我的所有代码复制到新的包中。所以我认为问题出在包本身。
【解决方案3】:

您可以从控制器中的会话中获取您的用户名。

 $userSession = new Container('user');
 $username = $userSession->username ;
 var_dump($username); //Sandhya

它对我有用。试试吧 !

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-22
    • 1970-01-01
    • 2013-06-14
    • 2017-12-22
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多