如何配置Zend \ Di

时间:2014-11-10 18:43:05

标签: zend-framework dependency-injection zend-framework2 zend-framework-mvc

我需要Zend \ Di的帮助;

我已经厌倦了使用Zend \ ServiceManager将工厂写入我的应用程序的任务,我想了解如何在Zend \ Di的MVC环境中进行配置。

我在module.config.php的顶部密钥di中看到了this post of @Ocramius的一些配置:

<?php

return array(
    'di' => array(
        'allowed_controllers' => array(
            // this config is required, otherwise the MVC won't even attempt to ask Di for the controller!
            'Application\Controller\GreetingController',
        ),

        'instance' => array(
            'preference' => array(
                // these allow injecting correct EventManager and ServiceManager
                // (taken from the main ServiceManager) into the controller,
                // because Di doesn't know how to retrieve abstract types. These
                // dependencies are inherited from Zend\Mvc\Controller\AbstractController
                'Zend\EventManager\EventManagerInterface' => 'EventManager',
                'Zend\ServiceManager\ServiceLocatorInterface' => 'ServiceManager',
            ),
        ),
    ),
    // remaining config
);

任何人都可以在此配置文档中发布网址或更详细的示例吗?

1 个答案:

答案 0 :(得分:1)

如果您有许多不同的控制器并且有很多服务可供选择,那么具有配置或注释的控制器应该是此链接中的选项:https://github.com/ralphschindler/Zend_DI-Examples

我认为你应该看一些像Aura.DiPimple这样的图书馆。它们提供了功能强大的DI容器。

如果您有最少的注入服务,请使用初始化程序。看看这个:http://akrabat.com/zend-framework-2/zendservicemanager-configuration-keys/