如何在 Zend Framewrok 2 中的会话中存储对象


how to store an object inside session in zend framewrok 2?

我想知道我是否可以使用 Zend Framework 2 在会话中存储对象

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

示例对象:

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

有什么想法吗?

在 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:当然,请确保会话可用