2017-04-10 3 views
1

Ich bekomme einen Fehler als;Zend Framework 2: Argument 1 an Album Controller AlbumController :: übergeben __ construct() muss eine Instanz von Album Controller AlbumTable sein

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 

Mein Modul.php ist;

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

Mein AlbumController ist wie;

<?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() 
{ 
} 
} 

Können Sie mir bitte sagen, was ich falsch mache? Ich bin sehr neu in Zend Framework. Es ist die Tutorial-Anwendung, die ich ausführen möchte.Ich habe alle Schritte befolgt, aber es gab viele Probleme und ich löste alle diese nacheinander, jetzt stecke ich hier fest.

+0

Was ist mit dieser Fehlermeldung in irgendeiner Weise kryptisch oder schwer zu verstehen? – RiggsFolly

+0

Ich habe den Fehler erwähnt. Es ist leicht zu verstehen, was der Fehler ist. Aber kann die Lösung nicht verstehen. –

+0

Fazit: 'muss eine Instanz von Album \ Controller \ AlbumTable, Instanz von Album \ Model \ AlbumTable gegeben werden – hassan

Antwort

1

Sie illegale Abhängigkeit verwenden sozusagen

// 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) 
); 

so dieses Problem zu lösen:

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

im Fall, wenn Sie Modell \ AlbumTable als Abhängigkeit verwenden müssen, so Sie müssen es wie folgt in Ihrem Controller einstellen:

use Model\AlbumTable as AlbumTableModel; 

... 
... 

public function __construct(AlbumTableModel $table) 
{ 
    $this->table = $table; 
} 
Verwandte Themen