dongxinpa3101
2012-11-22 11:18
浏览 66
已采纳

如何在ZF2中的Zend \ ServiceManager中使用Zend \ Di?

I'm trying to make the Zend\ServiceManager use Zend\Di to create my instances, since I have pre-scanned and cached DI definitions already. I realize this might come with a speed penalty but on the other hand, I need to write a lot less meta-code.

The ServiceManager documentation says that

the ServiceManager also provides optional ties to Zend\Di, allowing Di to act as an initializer or an abstract factory for the manager.

But I don't find any examples of how make the ServiceManager use Zend\Di. I'm not even sure where I should set this up, maybe in Module::getServiceConfig()? Can anyone provide some example code?

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

我正在尝试让Zend \ ServiceManager使用Zend \ Di创建我的实例,因为我已预先 扫描和缓存DI定义已经。 我意识到这可能会带来速度损失,但另一方面,我需要编写更少的元代码。

ServiceManager文档

ServiceManager还提供与Zend \ Di的可选绑定,允许Di 作为管理器的初始化器或抽象工厂。

但我不知道 找到如何使ServiceManager使用Zend \ Di的任何示例。 我甚至不确定我应该在哪里设置它,也许在Module :: getServiceConfig()中? 任何人都可以提供一些示例代码吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanba7498 2012-11-26 12:47
    已采纳

    The following works for me. In order to make Zend\Di compatible with Zend\ServiceManager, I extended a class MyLib\Di\Di from Zend\Di\Di which implements the AbstractFactoryInterface.

    namespace MyLib\Di;
    use Zend\ServiceManager\AbstractFactoryInterface; 
    use Zend\ServiceManager\ServiceLocatorInterface;
    
    class Di extends \Zend\Di\Di implements AbstractFactoryInterface
    {
        public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
        {
            return true;
        }
    
        public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
        {
            return $this->get($requestedName);
        }
    }
    

    Now, I can use MyLib\Di\Di as a fallback abstract factory for Zend\ServiceManager. Here's an example of how I create my IndexController. The IndexController's dependencies (constructor parameters) are injected automatically.

    class Module
    {
        ... 
    
        public function getServiceConfig()
        {        
            $this->di = new \MyLib\Di\Di;
            $this->configureDi($this->di); // Set up definitions and shared instances
    
            return array(
                'abstract_factories' => array($this->di),
            );
        }
    
        public function getControllerConfig()
        {
            return array(
                'factories' => array(
                    'Survey\Controller\IndexController' => function() {
                        return $this->di->get('Survey\Controller\IndexController');
                    },
                ),
            );
        }
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanbushi1867 2012-11-22 23:59

    One option - add to config/module.config.php

    'service_manager' => array(
        'invokables' => array(
            'Application\Service\User'              => 'Application\Service\User',
        ),
      ),
    

    then class needs to implement Zend\ServiceManager\ServiceManagerAwareInterface

    When initiated, serviceManager instance is going to be injected, then you can use something like this in class:

    $authService = $this->getServiceManager()->get('Zend\Authentication\AuthenticationService');
    

    second option would be to put it into Module.php

    public function getServiceConfig()
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题