0

我在 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 实例,以便访问其他服务?(大部分都是单例)

4

2 回答 2

1

ServiceManagerAwareInterface已弃用,并且不是自动注入
使用ServiceLocatorAwareInterface

于 2012-12-03T10:14:36.450 回答
1

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

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

if ($prototype instanceof ServiceManagerAwareInterface)
{
    $prototype->setServiceManager($sm);
}
于 2012-12-03T10:21:13.857 回答