2017-09-07 4 views
0

名前がのモジュールを作成しました。zend 3のコマースが正常に動作しています。私は__construct()を通じて依存関係を注入するとき今では /var/www/htmlと設定/ ZF3 /ベンダー/ zendframeworkに渡されたエラーzf3 __construct()が動作しません

少なすぎ コマース\コントローラ\ IndexControllerを関数の引数:: __構築物()、0を投げますライン30上の/zend-servicemanager/src/Factory/InvokableFactory.php と正確に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

あなたのエラーは、これはあなたのIndexControllerのコンストラクタに「コマース\モデル\コマース」を提供していないライン

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

に発生しています。あなたは依存関係を提供するために、これを変更する必要があります:

'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」の設定を「工場」で上で見たように、あなたの商工\モデル\コマースクラスのために自社工場を提供することです。

EDIT:あなたは、コントローラの工場内のすべてのものをしたい場合は、要求として、ここでは簡単な例です:

'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でそれらを定義する必要があるため、それは多くの作業のように見えます場所(コントローラとmodule.config.phpファイルの両方)。コントローラーでのみ定義しなければならない方法はありますか? – amitshree

+0

はい、あります。 "\ Commerce \ Model \ Commerce"のファクトリを定義するのをスキップすることができます。コントローラファクトリで作成するだけです:new Controller \ IndexController(new \ Commerce \ Model \ Commerce($ dependencies)); –

+0

この代替案を例として回答に追加できますか? – amitshree

関連する問題