首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >zf3 __construct()不工作

zf3 __construct()不工作
EN

Stack Overflow用户
提问于 2017-09-07 11:01:15
回答 1查看 1.6K关注 0票数 0

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

对于函数Commerce\Controller\IndexController::__construct(),0,在第30行的/var/www/html/zf3/vendor/zendframework/zend-servicemanager/src/Factory/InvokableFactory.php中传递的参数太少,而且完全是预期的1

这是控制器代码。

代码语言:javascript
复制
<?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代码

代码语言:javascript
复制
<?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已经安装好了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-07 11:18:10

您的错误是直线造成的。

代码语言:javascript
复制
Controller\IndexController::class => InvokableFactory::class

这并不为IndexController的构造函数提供"Commerce\Model\Commerce“。您需要更改它以提供依赖关系:

代码语言:javascript
复制
'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”的“工厂”设置所示。

编辑:根据请求,如果您想在控制器工厂中完成所有操作,下面是一个简单的示例:

代码语言:javascript
复制
'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)
            );
        },
    ],
],  
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46094684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档