zf3 __construct()不起作用

时间:2017-09-07 11:01:16

标签: zend-framework zend-framework3

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

  

功能参数太少   Commerce \ Controller \ IndexController :: __ construct(),0传入   /var/www/html/zf3/vendor/zendframework/zend-servicemanager/src/Factory/InvokableFactory.php   第30行,正好是预期的

这是控制器代码。

<?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已安装。

1 个答案:

答案 0 :(得分:1)

您的错误是在行中引起的

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类提供自己的工厂,如上面“service_manager”的“工厂”设置中所示。

编辑:作为请求,如果你想在控制器工厂内做所有事情,这里有一个简单的例子:

'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)
            );
        },
    ],
],