繁体   English   中英

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

[英]Zend 3 error: Argument to AlbumController::__construct() must be an instance of Album\Model\AlbumTable, none given

我正在使用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',
         ),
     ),
 );

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

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

看起来你在这里混淆了一些概念。 一个invokable将是一个在它的构造函数中没有参数的类。 ZF3中不再存在 invokable,就像它们在ZF2中那样。

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

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

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

您需要替换以下代码

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

   ),

与代码

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

文档中可以看出来

zf3中不再存在invokables。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM