zf3 __construct() 不工作

zf3 __construct() not working

我在 zend 3 中创建了一个名为 Commerce 的模块,它工作正常。现在,当我通过 __construct() 注入依赖项时,它会抛出错误

Too few arguments to function Commerce\Controller\IndexController::__construct(), 0 passed in /var/www/html/zf3/vendor/zendframework/zend-servicemanager/src/Factory/InvokableFactory.php on line 30 and exactly 1 expected

这是控制器代码。

<?php

namespace Commerce\Controller;

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

class IndexController extends AbstractActionController
{
    private $commerce;

    /**
     * IndexController constructor.
     * @param Commerce $commerce
     */
    public function __construct(Commerce $commerce)
    {
        $this->commerceModel = $commerce;
    }


    public function indexAction()
    {
    return new ViewModel();
    }

}

module.config.php代码

<?php

namespace Commerce;

use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;

return [
    'router' => [
        'routes' => [
            'home' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
            'commerce' => [
                'type'    => Segment::class,
                'options' => [
                    'route'    => '/commerce[/:action][/:id]',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
        ],
    ],
    'controllers' => [
        'factories' => [
            Controller\IndexController::class => InvokableFactory::class
        ],
    ],
    'view_manager' => [
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => [
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'commerce/index/index' => __DIR__ . '/../view/commerce/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ],
        'template_path_stack' => [
            __DIR__ . '/../view',
        ],
    ],
];

问题出在哪里? zend-di 已经安装。

你的错误是在

行引起的
Controller\IndexController::class => InvokableFactory::class

这不会向您的 IndexController 的构造函数提供 "Commerce\Model\Commerce"。您需要更改它以提供依赖项:

'controllers' => [
    'factories' => [
        Controller\IndexController::class => function($container) {
            return new Controller\IndexController(
                $container->get(\Commerce\Model\Commerce::class)
            );
        },
    ],
],
'service_manager' => [
    'factories' => [
        \Commerce\Model\Commerce::class =>  function($sm) {
            /* provide any dependencies if needed */
            /* Create the model here. */
            return new \Commerce\Model\Commerce($dependencies);
        },
    ]
],

最好的方法是为您的 Commerce\Model\Commerce class 提供自己的工厂,如上面 'service_manager' 的设置 'factories' 所示。

编辑:根据要求,如果您想在控制器工厂内完成所有操作,这里有一个简单的示例:

'controllers' => [
    'factories' => [
        Controller\IndexController::class => function($container) {
            $dbAdapter = $container->get('Zend\Db\Adapter\Adapter');
            $resultSetPrototype = new ResultSet();
            $tableGateway = new TableGateway('commerceTableName', $dbAdapter, null, $resultSetPrototype);

            return new Controller\IndexController(
                new \Commerce\Model\Commerce($tableGateway)
            );
        },
    ],
],