简体   繁体   中英

zf3 __construct() not working

I've created a module with name Commerce in zend 3 which is working fine. Now when I inject a dependency through __construct() it throws error

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

Here is the controller code.

<?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 code

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

Where is the issue? zend-di is already installed.

Your error is caused in line

Controller\IndexController::class => InvokableFactory::class

this does not provide a "Commerce\\Model\\Commerce" to the constructor of your IndexController. You need to change this to provide the dependency:

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

The best approach is to provide its own factory for your Commerce\\Model\\Commerce class, as seen above in the setting 'factories' of 'service_manager'.

EDIT: as request, if you want to do everything inside the controller Factory, here is a simple example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM