Zend 3错误:AlbumController :: __ construct()的参数必须是Album \ Model \ AlbumTable的一个实例,没有给出

时间:2016-11-01 07:13:54

标签: php zend-framework3

我正在使用zend 3,并且在尝试列出数据库表中的所有相册时无法解决此错误。 错误是

捕获致命错误:传递给Album \ Controller \ AlbumController :: __ construct()的参数1必须是Album \ Model \ AlbumTable的实例,没有给出,在C:\ xampp \ htdocs \ zftutorial \ vendor \ 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',
         ),
     ),
 );

2 个答案:

答案 0 :(得分:0)

在你的module.config.php中你有这个:

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

看起来你在这里混淆了一些概念。一个invokable将是一个没有参数的类的构造函数。 ZF3中的invokables no longer exist也与ZF2中的{{3}}相同。

由于控制器需要AlbumTable对象,因此您无法将其用作可调用对象,并且需要创建一个工厂来注入它。您已经在Module.php中完成了这项工作。

查看代码,如果从module.config.php中删除这些行,它可能会有效。

您还应该从Module.php中删除getAutoloaderConfig()。因为一段时间不推荐使用zend loader来支持composer autoloader。

答案 1 :(得分:0)

您需要替换以下代码

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

   ),

代码

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

来自docs

zf3中不再存在invokable。