将依赖项注入ZF2控制器时出错

时间:2016-04-13 10:56:27

标签: zend-framework2 servicemanager

嗨我在尝试在控制器上注入依赖项时遇到错误。

  

警告:缺少User \ Controller \ LoginController :: __ construct()的参数1,在第170行的/var/www/html/engsvc_dev/vendor/zendframework/zendframework/library/Zend/ServiceManager/AbstractPluginManager.php中调用在第23行的/var/www/html/engsvc_dev/module/User/src/User/Controller/LoginController.php中定义

Module.php

    public function getControllerConfig(){
    return array(
        'factories' => array(
            'Login' => function ($sm) {
                $locator = $sm->getServiceLocator();
                $controller = new LoginController($locator->get("Config"));
                return $controller;
            },
        ),
    );
}

控制器

class LoginController extends AbstractActionController{

protected $globalConfig;

protected $UserModuleSetup;

public function __construct($config){

}

module.config.php

    "invokables" => array(
    "User" => "User\Controller\LoginController",
    "Login" => "User\Controller\LoginController"
),

1 个答案:

答案 0 :(得分:0)

Module.php

    public function getControllerConfig(){
    return array(
        'factories' => array(
            'Login' => function ($sm) {
                $locator = $sm->getServiceLocator();
                $controller = new User\Controller\LoginController($locator->get("Config"));
                return $controller;
            },
        ),
    );
}

控制器

class LoginController extends AbstractActionController{

protected $globalConfig;

protected $UserModuleSetup;

public function __construct($config){

}

module.config.php

    "invokables" => array(
    "User" => "User\Controller\LoginController",
),