2017-09-07 23 views
0

我创建了一个名称为的商业在zend 3中工作正常。现在,当我通过__construct()注入依赖它抛出错误zf3 __construct()不起作用

参数太少运作 商务部\控制器\的IndexController :: __建设(),0 的/ var/www/html等/ ZF3 /供应商/ zendframework通过/zend-servicemanager/src/Factory/InvokableFactory.php 上线30和正好1预期

这里是控制器的代码。

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

你的错误是在线路

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

这不你的IndexController的构造提供了“商务部\型号\商”引起的。您需要更改此提供的依赖:

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

最好的办法是为您的商务\型号\商业类提供了自己的工厂,如在设置上面看到的“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) 
      ); 
     }, 
    ], 
], 
+0

它的工作,但它看起来像一个大量的工作,因为我必须在2来定义它们(在controller和module.config.php文件中)。有没有办法通过我只能在控制器中定义它们? – amitshree

+0

是的,有。您可以跳过为“\ Commerce \ Model \ Commerce”定义工厂,只需在控制器工厂中创建它:return new Controller \ IndexController(new \ Commerce \ Model \ Commerce($ dependencies)); –

+0

你可以在你的答案中加入这个选择吗? – amitshree