zend 2 inject to controller: Catchable fatal error: Argument 1 passed to someController::__construct() must be an instance of ... none given

zend 2 inject to controller: Catchable fatal error: Argument 1 passed to someController::__construct() must be an instance of ... none given

我有以下控制器:

namespace Application\Controller;

use Application\Model\Person;
use Zend\Mvc\Controller\AbstractActionController;
use Application\Model\PersonTable;

class PersonController extends AbstractActionController
{
    private $table;

    public function __construct(PersonTable $table)
    {
        $this->table = $table;
    }
    // other methods
}

我尝试按照此处的文档进行注入:

https://docs.zendframework.com/tutorials/getting-started/database-and-models/

在module/Application/Module.php中我添加了这个函数:

public function getControllerConfig()
    {
        return [
            'factories' => [
                Controller\PersonController::class => function($container) {
                    return new Controller\PersonController(
                        $container->get(Model\PersonTable::class)
                    );
                },
            ],
        ];
    }

在 module/Application/config/module.config.php 中我修改了这个,所以它会有我的控制器:

'controllers' => array(
        'invokables' => array(
            'Application\Controller\Index' => 'Application\Controller\IndexController',
            'Application\Controller\Person' => 'Application\Controller\PersonController'
        ),
    ),

我从控制台调用控制器方法并得到错误:

可捕获的致命错误:参数 1 传递给 Application\Controller\PersonControl ler::__construct() 必须是 Application\Controller\PersonTable 的实例,否 ne given, called in E:\other\dropbox\Dropbox\programavimas\kodo pavyzdziai\htdoc s\zend_2_staff_register\vendor\zendframework\zendframework\library\Zend\ServiceM anager\AbstractPluginManager.php 在第 170 行并在 E:\other\dropbox\Dro 中定义 pbox\programavimas\kodo pavyzdziai\htdocs\zend_2_staff_register\module\Applicati 在 \src\Application\Controller\PersonController.php 第 12

为什么没有注入?

因为你声明了你的

'Application\Controller\Person' => 'Application\Controller\PersonController'

作为可调用对象class,您需要将其设置为键:factories。

你是这样做的:

public function getControllerConfig()
    {
        return [
            'factories' => [
                Controller\PersonController::class => function($container) {
                    return new Controller\PersonController(
                        $container->get(Model\PersonTable::class)
                    );
                },
            ],
        ];
    }

Zf2 中的配置被合并,所以 PersonController 由最后的可调用程序加载,这段代码变得无用。

我建议你创建一个对象工厂而不是匿名函数并声明如下:

    'controllers' => array(
            'invokables' => array(
                'Application\Controller\Index' => 'Application\Controller\IndexController',
            ),
            'factories' => array(
                'Application\Controller\Person' => 'Application\Factory\PersonControllerFactory'
            ),
        ),

然后此对象 PersonControllerFactory 将 return 具有正确依赖项的控制器实例。

这里是控制器工厂的示例(与服务工厂不同): https://github.com/Grafikart/BlogMVC/blob/master/ZendFramework2/module/Blog/src/Blog/Factory/PostControllerFactory.php

及其配置行 https://github.com/Grafikart/BlogMVC/blob/master/ZendFramework2/module/Blog/config/module.config.controllers.php#L8