我使用 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?
感谢帮助使此会话容器正常工作。