2016-11-01 51 views
1

我正在使用zend 3並在嘗試列出數據庫表中的所有相冊時無法解決此錯誤。 錯誤是Zend 3錯誤:對AlbumController :: __ construct()的參數必須是Album Model AlbumTable的一個實例,沒有給出

可捕捉致命錯誤:傳遞給Album \ Controller \ AlbumController :: __ construct()的參數1必須是Album \ Model \ AlbumTable的實例,未給出,在C:\ xampp \ htdocs \ zftutorial中調用\供應商\ zendframework \ Zend的-的ServiceManager的\ src \廠\ InvokableFactory.php

這些都是我的文件: 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', 
     ), 
    ), 
); 

回答

0

在你module.config.php你有這樣的:

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

看起來你在這裏混淆了一些概念。一個invokeable可以是一個在其構造函數中沒有參數的類。 ZF3中也有invokables no longer exist,就像它們在ZF2中一樣。

由於控制器需要一個AlbumTable對象,因此不能將其用作可調用對象,並且需要創建一個工廠來注入它。你已經在Module.php中完成了。

看着你的代碼,它可能會工作,如果你從module.config.php中刪除這些行。

您還應該從Module.php中刪除getAutoloaderConfig()。一段時間以來,zend加載程序不贊成使用作曲家自動加載器。

0

您需要的代碼

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

由於從docs

invokables在ZF3不再存在替換下面的代碼

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

    ), 

相關問題