duanletao9487
2012-12-02 23:09
浏览 23
已采纳

无法访问Zend Framework 2中的ServiceManager

I'm on 2.0.4.

My files:

https://gist.github.com/4191490

According to the documentation at http://framework.zend.com/manual/2.0/en/modules/zend.service-manager.quick-start.html

If a class implements ServiceManagerAwareInterface, then its object will be initialized with the service manager. I did the same (see BaseEntity.php in my source code). However, the service manager was never initialised and is thus unavailable from the subclass Snippet.

Question: How to retrieve the global or module's ServireManager instance, so that I can access other services? (Most of them are singleton)

图片转代码服务由CSDN问答提供 功能建议

我在2.0.4。

我的文件:

https://gist.github.com/4191490

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

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

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dsc56927 2012-12-03 10:21
    已采纳

    For ServiceManagerAwareInterface to work as suggested the instance needs to come out of the ServiceManager. This is because there is an Initalizer attached to the ServiceManager that injects the ServiceManager if that interface is found (You can see this in - Zend\Mvc\Service\ServiceManagerConfig).

    You could add your classes to the SevriceManager configuration and then pull them through your $sm instance or even modify your getTableGateway to add the following:

    if ($prototype instanceof ServiceManagerAwareInterface)
    {
        $prototype->setServiceManager($sm);
    }
    
    已采纳该答案
    打赏 评论
  • duanlan7239 2012-12-03 10:14

    ServiceManagerAwareInterface is deprecated and is not aoutoinjected
    use ServiceLocatorAwareInterface instead

    打赏 评论

相关推荐 更多相似问题