简体   繁体   中英

ZF2 module config service_manager add constructor param

I've been searching and trying for a while now and I can't find out what I'm doing wrong... I've been reading quite some documentation now and have tried a lot of different approaches.

I hope one of you guys can help me out..

My idea: I have a module called Application. In this module there is an \\Application\\Controller\\IndexController which is used to show the homepage of my application. The IndexController needs an object with \\Zend\\http\\Client , so I though I should use the factories key in the configuration that is returned by \\Application\\Module->getServiceConfig() and add a constructor with required param \\Zend\\Http\\Client $client .

Unfortunatly this is not working..

My code:

<?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();
    }
}

The error:

Catchable fatal error: Argument 1 passed to Application\\Controller\\IndexController::__construct() must be an instance of Zend\\Http\\Client, none given, called in /var/www/library/Zend/ServiceManager/AbstractPluginManager.php on line 170 and defined in /var/www/module/Application/src/Application/Controller/IndexController.php on line 13

You need to implement the getControllerConfig method in Module.php to do what you're asking, controllers have their own manager, remove what you have from getServiceConfig, and use the following instead

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

You'll also need to remove the line from controller invokables in module.config.php since it'll conflict with your factory

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM