简体   繁体   English

可捕获的致命错误:传递给 Album\Controller\AlbumController::__construct() 的参数 1 必须是 Album\Model\AlbumTable 的实例,没有给出

[英]Catchable fatal error: Argument 1 passed to Album\Controller\AlbumController::__construct() must be an instance of Album\Model\AlbumTable, none given

I have just installed the project with the help of documentation;我刚刚在文档的帮助下安装了项目; and I am getting this error:我收到此错误:

ERROR错误

Catchable fatal error: Argument 1 passed to Album\Controller\AlbumController::__construct() must be an instance of Album\Model\AlbumTable, none given, called in C:\wamp64\www\myalbums\vendor\zendframework\zend-servicemanager\src\Factory\InvokableFactory.php on line 30 and defined in C:\wamp64\www\myalbums\module\Album\src\Controller\AlbumController.php on line 15可捕获的致命错误:传递给 Album\Controller\AlbumController::__construct() 的参数 1 必须是 Album\Model\AlbumTable 的实例,未给出,在 C:\wamp64\www\myalbums\vendor\zendframework\zend-servicemanager 中调用第 30 行的 \src\Factory\InvokableFactory.php 并在第 15 行的 C:\wamp64\www\myalbums\module\Album\src\Controller\AlbumController.php 中定义

module.config.php模块.config.php

<?php
namespace Album;

use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;

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


    // The following section is new and should be added to your file:
    'router' => [
        'routes' => [
            'album' => [
                'type'    => Segment::class,
                'options' => [
                    'route' => '/album[/:action[/:id]]',
                    'constraints' => [
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ],
                    'defaults' => [
                        'controller' => Controller\AlbumController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
        ],
    ],

    'view_manager' => [
        'template_path_stack' => [
            'album' => __DIR__ . '/../view',
        ],
    ],
];

AlbumTable.php相册表.php

namespace Album\Model;

use RuntimeException;
use Zend\Db\TableGateway\TableGatewayInterface;

class AlbumTable
{
    private $tableGateway;

    public function __construct(TableGatewayInterface $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]);
    }
}

AlbumController.php专辑控制器.php

namespace Album\Controller;

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

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()
    {
    }
}

The error comes from AlbumController constructor which expects an AlbumTable object to be injected when the former is created.该错误来自 AlbumController 构造函数,该构造函数期望在创建前者时注入 AlbumTable 对象。

The controller is created at控制器创建于

Controller\AlbumController::class => InvokableFactory::class,

using the standard factory with no constructor arguments.使用没有构造函数参数的标准工厂。

You need to change this to:您需要将其更改为:

Controller\AlbumController::class => function($container) {
    return new Controller\AlbumController(
         $container->get(\Album\Model\AlbumTable::class)
    );
},

so that the factory is provided the dependancy(AlbumTable).以便为工厂提供依赖项(AlbumTable)。

Also AlbumTable should have its own factory(maybe you already have this in your config): AlbumTable 也应该有自己的工厂(也许你的配置中已经有了):

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;

'service_manager' => [
        'factories' => [
            \Album\Model\AlbumTable::class =>  function($sm) {
                $tableGateway = $sm->get('AlbumTableGateway');
                $table = new \Album\Model\AlbumTable($tableGateway);
                return $table;
            },
            'AlbumTableGateway' => function ($sm) {
                $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                $resultSetPrototype = new ResultSet();
                return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
            },
        ]
    ],

where 'album' at TableGateway('album') is your database table name.其中TableGateway('album')的 'album' 是您的数据库表名。

References参考

Controller\AlbumController::class => InvokableFactory::class,

change to改成

 Controller\AlbumController::class => Controller\AlbumControllerFactory::class,

then create AlbumControllerFactory class in Controller dir然后在 Controller 目录中创建 AlbumControllerFactory 类

namespace Album\Controller;

use    Album\Controller\AlbumController;
use    Zend\ServiceManager\Factory\FactoryInterface;
use    Interop\Container\ContainerInterface;
use    Album\Model\AlbumTable;

class AlbumControllerFactory implements FactoryInterface {
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null){
        $model = $container->get(AlbumTable::class);
        return new AlbumController($model);
    }
}

also, if you don't have factory for AlbumModel you can create it same way so your modlue.config.php would look like for eg.此外,如果您没有 AlbumModel 的工厂,您可以以相同的方式创建它,这样您的 modlue.config.php 看起来就像例如。

....
'controllers' => [
    'factories' => [
        Controller\AlbumController::class => Controller\AlbumControllerFactory::class,
    ],
],
'service_manager' => [
    'factories' => [
        Model\AlbumModel::class => Model\AlbumModelFactory::class,
    ],
],
....

暂无
暂无

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

相关问题 Zend 3错误:AlbumController :: __ construct()的参数必须是Album \\ Model \\ AlbumTable的一个实例,没有给出 - Zend 3 error: Argument to AlbumController::__construct() must be an instance of Album\Model\AlbumTable, none given 可捕获的致命错误:传递给Controller :: __ construct()的参数1必须是Doctrine \\ ORM \\ EntityManager的实例,未给出任何实例,称为 - Catchable Fatal Error: Argument 1 passed to Controller::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called 可捕获的致命错误:传递给AppBundle \\ Form \\ TagType :: __ construct()的参数1必须是Doctrine \\ ORM \\ EntityRepository的实例,未给出任何实例, - Catchable Fatal Error: Argument 1 passed to AppBundle\Form\TagType::__construct() must be an instance of Doctrine\ORM\EntityRepository, none given, Symfony2:ContextErrorException:可捕获的致命错误:传递给[…] :: __ construct()的参数1必须实现接口[…]没有给出 - Symfony2: ContextErrorException: Catchable Fatal Error: Argument 1 passed to […]::__construct() must implement interface […] none given 可捕获的致命错误:传递给getPrice()的参数1必须是Rectangle的实例,没有给出 - Catchable fatal error: Argument 1 passed to getPrice() must be an instance of Rectangle, none given 可捕获的致命错误:传递给...的参数1必须是...,给定数组的实例 - Catchable Fatal Error: Argument 1 passed to … must be an instance of …, array given 可捕获的致命错误:传递给UsernamePasswordToken :: __ construct()的参数4必须是一个数组,给定null - Catchable Fatal Error: Argument 4 passed to UsernamePasswordToken::__construct() must be an array, null given 切换表时出现zend db错误可捕获的致命错误:传递给__construct()的参数1必须是一个数组,给定对象,在 - zend db error on switching tables Catchable fatal error: Argument 1 passed to __construct() must be an array, object given, called in 可捕获的致命错误:传递给UserBundle \\ Form \\ UserType :: __ construct()的参数2必须是实例? - Catchable Fatal Error: Argument 2 passed to UserBundle\Form\UserType::__construct() must be an instance ? 可捕获的致命错误:传递给 Illuminate\\Config\\Repository::__construct() 的参数 1 必须是数组类型,给定整数 - Catchable fatal error: Argument 1 passed to Illuminate\Config\Repository::__construct() must be of the type array, integer given
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM