2016-11-01 49 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', 
    ),  

    ), 

相关问题