Symfony1 存储会话数据
关于在Symfony中存储会话数据,我有一些反问。 我们可以将会话数据存储为变量:Symfony1 存储会话数据,symfony1,symfony-1.4,Symfony1,Symfony 1.4,关于在Symfony中存储会话数据,我有一些反问。 我们可以将会话数据存储为变量: // The First Example $this->getUser()->setAttribute('hash', $hash); $this->getUser()->setAttribute('name', $name); 或作为数组: // The Second Example $this->getUser()->setAttribute('something'
// The First Example
$this->getUser()->setAttribute('hash', $hash);
$this->getUser()->setAttribute('name', $name);
或作为数组:
// The Second Example
$this->getUser()->setAttribute('something'
, array('hash' => $hash,'name' => $name));
在第一个示例中,我们可以使用hasAttribute('name')来检查它是否已设置,在第二个示例中,我们需要两行代码来进行此类检查。例如,hasAttribute(“名称”)等方法将不起作用:
$something = $this->getUser()->getAttribute('something');
if($something['name']) //...
此外,将新值设置为变量需要更多行:
$something['name'] = 'New value';
$this->getUser()->setAttribute('something', $something);
但是,拥有用于存储会话的阵列的好处是能够一次清除整个阵列
也许有可能以我不知道的更好的方式操纵数组?
或者也许我的陈述完全错了。。。
最佳做法是什么?您可以添加名称空间来存储数据:
$this->getUser()->setAttribute($name, $value, $namespace);
要检索数据,还需要使用名称空间:
$this->getUser()->getAtrribute($name, $default, $namespace);
$this->getUser()->hasAttribute($name, $namespace);
您还可以检查用户是否具有带有名称空间的属性:
$this->getUser()->getAtrribute($name, $default, $namespace);
$this->getUser()->hasAttribute($name, $namespace);
symfony将把值作为数组存储到名称空间中。太棒了!我不知道那个精彩的特写!谢谢你的帮助!是否可以按特定命名空间清除所有会话数据?