ZF2模块配置service_manager添加构造函数参数

时间:2013-03-08 15:03:10

标签: dependency-injection zend-framework2 php

我一直在寻找和尝试一段时间,我无法找出我做错了什么...... 我现在已经阅读了很多文档,并尝试了很多不同的方法。

我希望你们其中一个人可以帮助我......

我的想法: 我有一个名为Application的模块。在此模块中有一个\Application\Controller\IndexController,用于显示我的应用程序的主页。 IndexController需要一个\Zend\http\Client的对象,所以我应该在\Application\Module->getServiceConfig()返回的配置中使用factories键,并添加一个带有必需参数\Zend\Http\Client $client的构造函数。

不幸的是,这不起作用..

我的代码:

<?php
// module/Application/Module.php

namespace Application;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

class Module
{
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'IndexController' => function($serviceManager) {
                    $httpClient = \Zend\Http\Client;

                    return new IndexController($httpClient);
                },
            ),
        );
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
}

<?php
// module/Application/config/module.config.php

return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route' => '/',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action' => 'index',
                    ),
                ),
            )
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
        ),
    ),
    'translator' => array(
        'locale' => 'en_US',
        'translation_file_patterns' => array(
            array(
                'type' => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern' => '%s.mo',
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Application\Controller\Index' => 'Application\Controller\IndexController'
        ),
    ),
    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions' => true,
        'doctype' => 'HTML5',
        'not_found_template' => 'error/404',
        'exception_template' => 'error/index',
        'template_map' => array(
            'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
            'application/index/index' => __DIR__ . '/../view/index/index.phtml',
            'error/404' => __DIR__ . '/../view/error/404.phtml',
            'error/index' => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
);

<?php
// module/Application/src/Application/Controller/IndexController.php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class IndexController extends AbstractActionController
{
    protected $httpClient;

    public function __construct(\Zend\Http\Client $client)
    {
        $this->httpClient = $client;
    }

    public function getHttpClient()
    {
        return $this->httpClient;
    }

    public function indexAction()
    {                
        $httpClient = $this->getHttpClient();

        return new ViewModel();
    }
}

错误:

  

捕获致命错误:传递给Application \ Controller \ IndexController :: __ construct()的参数1必须是Zend \ Http \ Client的实例,没有给出,在/ var / www / library / Zend / ServiceManager / AbstractPluginManager中调用。第170行的php并在第13行的/var/www/module/Application/src/Application/Controller/IndexController.php中定义

1 个答案:

答案 0 :(得分:3)

您需要在Module.php中实现getControllerConfig方法来执行您所要求的操作,控制器拥有自己的管理器,从getServiceConfig中删除您拥有的内容,然后使用以下代码

public function getControllerConfig()
{
    return array(
        'factories' => array(
            'Application\Controller\Index' => function($serviceManager) {
                $httpClient = new \Zend\Http\Client;
                return new \Application\Controller\IndexController($httpClient);
            },
        ),
    );
}

你还需要从module.config.php中的控制器invokable中删除该行,因为它会与你的工厂冲突

'controllers' => array(
    'invokables' => array(
        // this line's no longer needed
        //'Application\Controller\Index'      => 'Application\Controller\IndexController',
    ),
),