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)

  • 写回答

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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器