无法访问Zend Framework 2中的ServiceManager

时间:2012-12-02 23:09:03

标签: php frameworks zend-framework2 servicemanager

我在2.0.4。

我的档案:

https://gist.github.com/4191490

根据http://framework.zend.com/manual/2.0/en/modules/zend.service-manager.quick-start.html

的文件

如果一个类实现了ServiceManagerAwareInterface,那么它的对象将使用服务管理器进行初始化。我做了同样的事情(参见我的源代码中的BaseEntity.php)。但是,服务管理器从未初始化,因此无法从子类Snippet中获取。

问题:如何检索全局或模块的ServireManager实例,以便我可以访问其他服务? (大多数都是单身人士)

2 个答案:

答案 0 :(得分:1)

ServiceManagerAwareInterface已被弃用,不会被注销 使用ServiceLocatorAwareInterface代替

答案 1 :(得分:1)

要使ServiceManagerAwareInterface按照建议工作,实例需要来自ServiceManager。这是因为如果找到该接口,就会在注入ServiceManager的ServiceManager附加一个Initalizer(您可以在 - Zend\Mvc\Service\ServiceManagerConfig中看到这一点。)

您可以将您的类添加到SevriceManager配置中,然后通过$sm实例提取它们,甚至修改您的getTableGateway以添加以下内容:

if ($prototype instanceof ServiceManagerAwareInterface)
{
    $prototype->setServiceManager($sm);
}