如何使用ZF3访问会话容器?

时间:2016-12-05 13:43:58

标签: php session zend-framework zend-framework3 zf3

我使用ZF3,我正在构建一个服务,它将提供对应用程序会话容器的应用程序范围的分段访问。此服务称为SessionContainerManager,将具有检索和更新用户身份,用户ACL等的方法。我的代码:

namespace User\Service;

class SessionContainerManager
{
    /**
     * Service container.
     * @var Zend\Service\Container
     */
    private $sessionContainer;


    public function __construct($sessionContainer) 
    {
        $this->sessionContainer = $sessionContainer;
    }

    public function getACLList() 
    {
        return $this->sessionContainer->aclList;
    }

    public function setACLList($aclList) 
    {
        $this->sessionContainer->aclList = $aclList;
    }

    public function getIdentity() 
    {
        return $this->sessionContainer->identity;
    }

    public function setIdentity($identity) 
    {
        $this->sessionContainer->identity = $identity;
    }

}

它的工厂:

namespace User\Service\Factory;

use Interop\Container\ContainerInterface;
use User\Service\SessionContainerManager;
use Zend\Session\Container;

class SessionContainerManagerFactory
{
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {        
        $sessionContainer = $container->get(\Zend\Session\Container::class);

        return new SessionContainerManager($sessionContainer);
    }
}

在module.config中:

'service_manager' => [
    'factories' => [
        Service\SessionContainerManager::class => Service\Factory\SessionContainerManagerFactory::class,
    ],

运行应用程序时,我收到以下错误:

File:

    /home/renato/project/dev/fways/php/fways/vendor/zendframework/zend-servicemanager/src/ServiceManager.php:670

Message:

    Unable to resolve service "Zend\Session\Container" to a factory; are you certain you provided it during configuration?

帮助我们了解这个会话容器的工作原理。

2 个答案:

答案 0 :(得分:0)

点击此链接working with Sessions in zf3 可能你首先没有得到这个包。如果它不在您的作曲家中,请尝试将其添加:

php composer.phar require zendframework/zend-session

答案 1 :(得分:-1)

由缓存的模块配置引起的。它是在第一时间生成的,以加快读取配置。因此,添加新服务配置后,请始终删除data/cache/module-config-cache.application.config.cache.php中的缓存。如果找不到,将自动创建。