【问题标题】:how to store an object inside session in zend framewrok 2?如何在 zend 框架 2 中的会话中存储对象?
【发布时间】:2013-10-21 22:37:49
【问题描述】:

我想知道是否可以使用 zend 框架 2 在会话中存储对象

当我检索对象时,它需要是一个对象而不是数组

示例对象:

object(Zend\Stdlib\Parameters)[144]
  public 'name' => string 'test test' (length=9)
  public 'website' => string 'test.com' (length=8)
  ...

有什么想法吗?

【问题讨论】:

    标签: php session object zend-framework2 zend-session


    【解决方案1】:

    在 Zend Framework 2 中,会话似乎是一个关联数组,键为 namespace,值为另一个关联数组。

    为了操纵会话,您可以使用一个名为Container的抽象层

    use Zend\Session\Container;
    
    // namespace 'user'
    $userContainer = new Container('user');
    
    // Store the locale and devise
    $userContainer->locale = 'fr-FR';
    $userContainer->devise = 'Euro';
    

    在特定的命名空间下写你想要的。 您可以稍后通过以下方式检索数据:

    use Zend\Session\Container;
    
    // Create a container to manipulate session data
    $userContainer = new Container('user');
    
    // Check if the data exist under the namespace
    if ( $userContainer->offsetExists('devise'))
    {
        // Retrieve the data
        $devise = $userContainer->offsetGet('devise');
    }
    else
    {
        // Get the default value
        $devise = 'Euro';
    }
    

    PS:当然,请确保会话可用

    【讨论】:

    • locale 和 devise 是例子,你可以存储你想要的
    猜你喜欢
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 2012-06-16
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多