Zend framework2 ZF2:访问存储服务另一个模块有空容器?
我有两个模块:应用程序和管理器。应用程序模块有AuthStorage服务,我使用一个容器来存储用户级别(admin或user) 但是,当从管理器模块访问Application\Model\AuthStorage中的容器时,该容器似乎是空的 从另一个模块调用时是否正在创建新实例?(看起来是这样的) AuthStorage service实例的范围是否仅限于应用程序模块 或者如何全局访问用户级容器 以下是我如何从Manager模块访问存储服务:Zend framework2 ZF2:访问存储服务另一个模块有空容器?,zend-framework2,Zend Framework2,我有两个模块:应用程序和管理器。应用程序模块有AuthStorage服务,我使用一个容器来存储用户级别(admin或user) 但是,当从管理器模块访问Application\Model\AuthStorage中的容器时,该容器似乎是空的 从另一个模块调用时是否正在创建新实例?(看起来是这样的) AuthStorage service实例的范围是否仅限于应用程序模块 或者如何全局访问用户级容器 以下是我如何从Manager模块访问存储服务: $this->storage = $this-&
$this->storage = $this->getServiceLocator()
->get('Application\Model\AuthStorage');
Module.php getServiceConfig()
AuthStorage类
namespace Application\Model;
use Zend\Authentication\Storage;
use Zend\Session\Container;
class AuthStorage extends Storage\Session
{
protected $container;
public function setRememberMe($rememberMe = 0, $time = 1209600)
{
if ($rememberMe == 1) {
$this->session->getManager()->rememberMe($time);
}
}
public function forgetMe()
{
$this->session->getManager()->forgetMe();
}
public function getContainer()
{
if (!isset($this->container)) {
$this->container = new Container('authsessionstorage');
$this->container->userlevel = 0;
}
return $this->container;
}
public function setUserLevel($userlevel)
{
$_container = $this->getContainer();
$_container->userlevel = $userlevel;
}
public function getUserLevel()
{
$_container = $this->getContainer();
return $_container->userlevel;
}
public function isAdministrator() {
echo '<pre>', 'userlevel = ' . print_r($this->getUserLevel(), true), '</pre>';
if ($this->getUserLevel() == 100) {
return true;
}
return false;
}
}
名称空间应用程序\模型;
使用Zend\Authentication\Storage;
使用Zend\Session\Container;
类AuthStorage扩展存储\会话
{
受保护的集装箱;
公共函数setRememberMe($rememberMe=0,$time=1209600)
{
如果($rememberMe==1){
$this->session->getManager()->rememberMe($time);
}
}
公共职能
{
$this->session->getManager()->forgetMe();
}
公共函数getContainer()
{
如果(!isset($this->container)){
$this->container=新容器('authsessionstorage');
$this->container->userlevel=0;
}
返回$this->container;
}
公共函数setUserLevel($userlevel)
{
$\u container=$this->getContainer();
$\u container->userlevel=$userlevel;
}
公共函数getUserLevel()
{
$\u container=$this->getContainer();
返回$容器->用户级别;
}
公共职能isAdministrator(){
回显“”,'userlevel='。打印($this->getUserLevel(),true),“”;
$this->container = new Container('authsessionstorage');
如果($this->getUserLevel()==100){
返回true;
}
返回false;
}
}
Zend\Session\Container只是一个用于处理PHP$\u会话的接口,因此它在ZF2中不是特定于模块的
这条线
$this->container->userlevel = 0;
正在设置名为authsessionstorage的容器
这条线,
$container = new Container('authsessionstorage');
echo $container->userlevel;
正在将键/值设置为userlevel/0
要在其他任何地方检索该键的值,只需执行以下操作
在任何人都能正确回答您的问题之前,您需要显示
AuthStorage
的代码。现在更新。相同的容器代码在应用程序模块中(在AuthController中)可以正常工作,但在管理器模块的ManagerController中则不行。
$container = new Container('authsessionstorage');
echo $container->userlevel;