Zend Framework 2:传递给 Album\Controller\AlbumController::__construct() 的参数 1 必须是 Album\Controller\AlbumTable 的实例 [英] Zend Framework 2:Argument 1 passed to Album\Controller\AlbumController::__construct() must be an instance of Album\Controller\AlbumTable

查看:19
本文介绍了Zend Framework 2:传递给 Album\Controller\AlbumController::__construct() 的参数 1 必须是 Album\Controller\AlbumTable 的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个错误:;

Argument 1 passed to Album\Controller\AlbumController::__construct() must be an instance of Album\Controller\AlbumTable, instance of Album\Model\AlbumTable given,, called in /var/www/html/zf/module/Album/src/Module.php on line 43

我的 Module.php 是;

My Module.php is;

<?php
  namespace Album;

  use Zend\ModuleManager\Feature\ConfigProviderInterface;
  use Zend\Db\Adapter\AdapterInterface;
  use Zend\Db\ResultSet\ResultSet;
  use Zend\Mvc\ModuleRouteListener;
  use Zend\Mvc\MvcEvent;
  use Zend\Db\TableGateway\TableGateway;

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

// Add this method:
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 Model\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)
                 );
             },
          ],
      ];
   }
}

我的相册控制器就像;

<?php
namespace Album\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

use Album\Model;

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 框架很陌生.这是我正在尝试运行的教程应用程序.我遵循了所有步骤,但出现了很多问题,我一一解决了所有这些问题,现在我被困在这里.

Can you please tell me what I am doing wrong? I am very new in Zend Framework. It is the tutorial application I am trying to run.I followed all steps but there were a lot of issues and I solved all those one by one , now I am stuck here.

推荐答案

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

you are using illegal dependency so to speak,

// Here you are returning Model\AlbumTable object
// while your Controller\AlbumController needs a Controller\AlbumTable instance
return new Controller\AlbumController(
    $container->get(Model\AlbumTable::class)
);

所以要解决这个问题:

return new Controller\AlbumController(
    $container->get(Controller\AlbumTable::class)
);

<小时>

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


in case if you need to use Model\AlbumTable as a dependency, so you will need to set it in your controller as follows :

use Model\AlbumTable as AlbumTableModel;

...
...

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

这篇关于Zend Framework 2:传递给 Album\Controller\AlbumController::__construct() 的参数 1 必须是 Album\Controller\AlbumTable 的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆