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 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应