2016-11-01 3 views
1

Ich benutze zend 3 und kann diesen Fehler nicht lösen, wenn ich versuche, alle Alben aus der Datenbanktabelle aufzulisten. Der Fehler istZend 3 Fehler: Argument zu AlbumController :: __ construct() muss eine Instanz von Album Model AlbumTable sein, keine gegeben

Abfangbare fatale Fehler: Argument 1 bis Album \ Regler \ AlbumController geben :: __ construct() muß in C eine Instanz von Album \ Modell \ AlbumTable, nicht angegeben, bezeichnet werden: \ xampp \ htdocs \ zftutorial \ Anbieter \ ZendFramework \ zend-Servicemanager \ src \ Fabrik \ InvokableFactory.php

Das sind meine Dateien: AlbumController.php

<?php 
namespace Album\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 
use Album\Model\AlbumTable; 
use Zend\Db\Adapter\AdapterInterface; 


class AlbumController extends AbstractActionController 
{ 


    private $table; 

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


} 

AlbumTable.php

<?php 
namespace Album\Model; 

use RuntimeException; 
use Zend\Db\TableGateway\TableGateway; 

class AlbumTable 
{ 
    protected $tableGateway; 

    public function __construct(TableGateway $tableGateway) 
    { 
     $this->tableGateway = $tableGateway; 
    } 

    public function fetchAll() 
    { 
     return $this->tableGateway->select(); 
    } 

    public function getAlbum($id) 
    { 
     $id = (int) $id; 
     $rowset = $this->tableGateway->select(['id' => $id]); 
     $row = $rowset->current(); 
     if (! $row) { 
      throw new RuntimeException(sprintf(
       'Could not find row with identifier %d', 
       $id 
      )); 
     } 

     return $row; 
    } 

    public function saveAlbum(Album $album) 
    { 
     $data = [ 
      'artist' => $album->artist, 
      'title' => $album->title, 
     ]; 

     $id = (int) $album->id; 

     if ($id === 0) { 
      $this->tableGateway->insert($data); 
      return; 
     } 

     if (! $this->getAlbum($id)) { 
      throw new RuntimeException(sprintf(
       'Cannot update album with identifier %d; does not exist', 
       $id 
      )); 
     } 

     $this->tableGateway->update($data, ['id' => $id]); 
    } 

    public function deleteAlbum($id) 
    { 
     $this->tableGateway->delete(['id' => (int) $id]); 
    } 
} 

module.php

<?php 
namespace Album; 

use Zend\ModuleManager\Feature\AutoloaderProviderInterface; 
use Album\Model\Album; 
use Album\Model\AlbumTable; 
use Zend\Db\Adapter\AdapterInterface; 
use Zend\Db\ResultSet\ResultSet; 
use Zend\Db\TableGateway\TableGateway; 
use Zend\ModuleManager\Feature\ConfigProviderInterface; 

//use \Zend\Mvc\Controller\ControllerManager; 


class Module implements AutoloaderProviderInterface, ConfigProviderInterface 

{ 
    public function getAutoloaderConfig() 
    { 
     return array(
      'Zend\Loader\ClassMapAutoloader' => array(
       __DIR__ . '/autoload_classmap.php', 
      ), 
      'Zend\Loader\StandardAutoloader' => array(
       'namespaces' => array(
        __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, 
       ), 
      ), 
     ); 
    } 

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




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

module.config.php

<?php 
namespace Album; 
return array(
    'controllers' => array(
     'invokables' => array(
      'Album\Controller\Album' => 'Album\Controller\AlbumController', 
     ),  

    ), 


    'router' => array(
     'routes' => array(
      'album' => array(
       'type' => 'segment', 
       'options' => array(
        'route' =>'/album[/][:action][/:id]', 

        'constraints' => array(
         'action' => '[a-zA-Z][a-zA-Z0-9_-]*', 
         'id'  => '[0-9]+', 
        ), 
        'defaults' => array(
         'controller' => 'Album\Controller\Album', 
         'action'  => 'index', 
        ), 
       ), 
      ), 
     ), 
    ), 
    'view_manager' => array(
     'template_path_stack' => array(
      'album' => __DIR__ . '/../view', 
     ), 
    ), 
); 

Antwort

0

In Ihrem module.config.php Sie haben dies:

'controllers' => array(
    'invokables' => array(
     'Album\Controller\Album' => 'Album\Controller\AlbumController', 
    ),  
), 

Es sieht so aus, als ob Sie hier ein paar Konzepte verwechseln. Ein Invokable wäre eine Klasse ohne Parameter im Konstruktor. Auch in ZF3 wie in ZF2.

Da der Controller ein AlbumTable-Objekt benötigt, können Sie es nicht als aufrufbar verwenden und müssen eine Factory erstellen, um es zu injizieren. Was hast du schon in Module.php gemacht.

Betrachtet man den Code, könnte es funktionieren, wenn Sie diese Zeilen aus module.config.php entfernen.

Sie sollten auch die getAutoloaderConfig() aus Module.php entfernen. Seit einiger Zeit ist der Zend Loader zugunsten des Composer Autoloaders veraltet.

0

Sie müssen den Code unten

'controllers' => array(
    'invokables' => array(
     'Album\Controller\Album' => 'Album\Controller\AlbumController', 
    ),  

    ), 

mit dem Code

'controllers' => [ 
     'factories' => [ 
     Controller\AlbumController::class => InvokableFactory::class, 
    ], 
    ], 

Wie aus dem docs

invokables ersetzen ist nicht mehr in ZF3.

Verwandte Themen