Zend Framework 2:传递给Album Controller AlbumController :: __ construct()的参数1必须是Album Controller (代码

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Zend Framework 2:传递给Album Controller AlbumController :: __ construct()的参数1必须是Album Controller (代码相关的知识,希望对你有一定的参考价值。

我得到一个错误;

Argument 1 passed to AlbumControllerAlbumController::__construct() must be an instance of AlbumControllerAlbumTable, instance of AlbumModelAlbumTable given,, called in /var/www/html/zf/module/Album/src/Module.php on line 43

我的Module.php是;

<?php
  namespace Album;

  use ZendModuleManagerFeatureConfigProviderInterface;
  use ZendDbAdapterAdapterInterface;
  use ZendDbResultSetResultSet;
  use ZendMvcModuleRouteListener;
  use ZendMvcMvcEvent;
  use ZendDbTableGatewayTableGateway;

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

// Add this method:
public function getServiceConfig()
{
    return [
        'factories' => [
            ModelAlbumTable::class => function($container) {
                $tableGateway = $container->get(ModelAlbumTableGateway::class);
                return new ModelAlbumTable($tableGateway);
            },
            ModelAlbumTableGateway::class => function ($container) {
                $dbAdapter = $container->get(AdapterInterface::class);
                $resultSetPrototype = new ResultSet();
                $resultSetPrototype->setArrayObjectPrototype(new ModelAlbum());
                return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
            },
        ],
    ];
}

public function getControllerConfig()
{
   return [
           'factories' => [
           ControllerAlbumController::class => function($container) {
                 return new ControllerAlbumController(
                     $container->get(ModelAlbumTable::class)
                 );
             },
          ],
      ];
   }
}

我的AlbumController就像;

<?php
namespace AlbumController;

use ZendMvcControllerAbstractActionController;
use ZendViewModelViewModel;

use AlbumModel;

class AlbumController extends AbstractActionController
{
// Add this property:
private $table;

// Add this constructor:
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()
{
}
}

你能告诉我我做错了什么吗?我是Zend Framework的新手。这是我试图运行的教程应用程序。我遵循了所有步骤,但是有很多问题,我逐一解决了所有问题,现在我被困在这里。

答案

你正在使用非法依赖,可以这么说

// Here you are returning ModelAlbumTable object
// while your ControllerAlbumController needs a ControllerAlbumTable instance
return new ControllerAlbumController(
    $container->get(ModelAlbumTable::class)
);

所以解决这个问题:

return new ControllerAlbumController(
    $container->get(ControllerAlbumTable::class)
);

如果您需要使用Model AlbumTable作为依赖项,那么您需要在控制器中设置它,如下所示:

use ModelAlbumTable as AlbumTableModel;

...
...

public function __construct(AlbumTableModel $table)
{
    $this->table = $table;
}

以上是关于Zend Framework 2:传递给Album Controller AlbumController :: __ construct()的参数1必须是Album Controller (代码的主要内容,如果未能解决你的问题,请参考以下文章

如何模拟 Zend\Form 提交而不在 Zend Framework 2/3 中显示表单?

如何模拟Zend Form提交而不在Zend Framework 2/3中显示表单?

Zend Framework:从路由获取子域参数

jquery FormData和Zend Framework 2

Using zend-navigation in your Album Module

使用 zend 2 的 tableGateway 进行插入