2017-04-10 35 views
1

我得到一個錯誤;Zend Framework 2:傳遞給Album Controller AlbumController :: __ construct()的參數1必須是Album Controller AlbumTable的一個實例

Argument 1 passed to Album\Controller\AlbumController::__construct() must be an instance of Album\Controller\AlbumTable, instance of Album\Model\AlbumTable given,, called in /var/www/html/zf/module/Album/src/Module.php on line 43 

My Module.php is;

<?php 
    namespace Album; 

    use Zend\ModuleManager\Feature\ConfigProviderInterface; 
    use Zend\Db\Adapter\AdapterInterface; 
    use Zend\Db\ResultSet\ResultSet; 
    use Zend\Mvc\ModuleRouteListener; 
    use Zend\Mvc\MvcEvent; 
    use Zend\Db\TableGateway\TableGateway; 

class Module implements ConfigProviderInterface 
{ 
public function getConfig() 
{ 
    return include __DIR__ . '/../config/module.config.php'; 
} 

// Add this method: 
public function getServiceConfig() 
{ 
    return [ 
     'factories' => [ 
      Model\AlbumTable::class => function($container) { 
       $tableGateway = $container->get(Model\AlbumTableGateway::class); 
       return new Model\AlbumTable($tableGateway); 
      }, 
      Model\AlbumTableGateway::class => function ($container) { 
       $dbAdapter = $container->get(AdapterInterface::class); 
       $resultSetPrototype = new ResultSet(); 
       $resultSetPrototype->setArrayObjectPrototype(new Model\Album()); 
       return new TableGateway('album', $dbAdapter, null, $resultSetPrototype); 
      }, 
     ], 
    ]; 
} 

public function getControllerConfig() 
{ 
    return [ 
      'factories' => [ 
      Controller\AlbumController::class => function($container) { 
       return new Controller\AlbumController(
        $container->get(Model\AlbumTable::class) 
       ); 
      }, 
      ], 
     ]; 
    } 
} 

我的AlbumController就像;

<?php 
namespace Album\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 

use Album\Model; 

class AlbumController extends AbstractActionController 
{ 
// Add this property: 
private $table; 

// Add this constructor: 
public function __construct(AlbumTable $table) 
{ 
    $this->table = $table; 
} 

public function indexAction() 
{ 
    return new ViewModel([ 
     'albums' => $this->table->fetchAll(), 
    ]); 
} 

public function addAction() 
{ 
} 

public function editAction() 
{ 
} 

public function deleteAction() 
{ 
} 
} 

能否請你告訴我什麼,我做錯了什麼?我在Zend Framework中很新。這是我試圖運行的教程應用程序。我遵循所有步驟,但有很多問題,我一一解決了所有這些問題,現在我被困在這裏。

+0

那麼,那個錯誤信息怎麼樣都難以理解? – RiggsFolly

+0

我已經提到了這個錯誤。很容易理解錯誤是什麼。但無法理解解決方案。 –

+0

結論:'必須是Album \ Controller \ AlbumTable的一個實例,Album \ Model \ AlbumTable的實例' – hassan

回答

1

你正在使用非法的依賴可以這麼說,

// Here you are returning Model\AlbumTable object 
// while your Controller\AlbumController needs a Controller\AlbumTable instance 
return new Controller\AlbumController(
    $container->get(Model\AlbumTable::class) 
); 

所以要解決這個問題:

return new Controller\AlbumController(
    $container->get(Controller\AlbumTable::class) 
); 

的情況下,如果你需要使用型號\ AlbumTable作爲依賴,所以您需要將其設置在控制器中,如下所示:

use Model\AlbumTable as AlbumTableModel; 

... 
... 

public function __construct(AlbumTableModel $table) 
{ 
    $this->table = $table; 
} 
相關問題