Php ZF 3__construct()必须是。。。没有给出

Php ZF 3__construct()必须是。。。没有给出,php,zend-db,zend-framework3,Php,Zend Db,Zend Framework3,我得到一个错误: Catchable fatal error: Argument 1 passed to Application\Controller\IndexController::__construct() must be an instance of Application\Model\PlatnosciTable, none given, called in /var/www/Paczucha_pl/vendor/zendframework/zend-servicemanager/sr

我得到一个错误:

Catchable fatal error: Argument 1 passed to Application\Controller\IndexController::__construct() must be an instance of Application\Model\PlatnosciTable, none given, called in /var/www/Paczucha_pl/vendor/zendframework/zend-servicemanager/src/Factory/InvokableFactory.php on line 32 and defined in /var/www/Paczucha_pl/module/Application/src/Controller/IndexController.php on line 21
我试图找出我的代码中有什么地方做错了。名字刚从Album改为Platnosci

这是我的文件:

Platnosci.php

namespace Application\Model;

class Platnosci
{
    public $id;
    public $artist;
    public $title;

    public function exchangeArray($data)
    {
        $this->id     = (!empty($data['id'])) ? $data['id'] : null;
        $this->artist = (!empty($data['artist'])) ? $data['artist'] : null;
        $this->title  = (!empty($data['title'])) ? $data['title'] : null;
    }
}
platnositable.php

namespace Application\Model;

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

class PlatnosciTable
{
    protected $tableGateway;

    public function __construct(TableGateway $tableGateway) {
        $this->tableGateway = $tableGateway;
    }

    public function fetchAll() {
        $resultSet = $this->tableGateway->select();
        return $resultSet;
    }

    public function getAlbum($id) {
        $id  = (int) $id;
        $rowset = $this->tableGateway->select(array('id' => $id));
        $row = $rowset->current();
        if (!$row) {
            throw new \Exception("Could not find row $id");
        }
        return $row;
    }

    public function saveAlbum(Platnosci $album) {
        $data = array(
            'artist' => $album->artist,
            'title'  => $album->title,
        );

        $id = (int) $album->id;
        if ($id == 0) {
            $this->tableGateway->insert($data);
        } else {
            if ($this->getAlbum($id)) {
                $this->tableGateway->update($data, array('id' => $id));
            } else {
                throw new \Exception('Album id does not exist');
            }
        }
    }

    public function deleteAlbum($id) {
        $this->tableGateway->delete(array('id' => (int) $id));
    }
}
IndexController.php

namespace Application\Controller;

use Application\Model\Przesylki;
use Application\Model\PlatnosciTable;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Db\Adapter\AdapterInterface;
use Zend\View\Model\ViewModel;


class IndexController extends AbstractActionController
{
    private $table;

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

    public function indexAction()
    {
        $this->layout('layout/layout');
    }

    public function counterAction() {
        $this->layout('layout/counter');
    }
    public function cennikAction() {
        return new ViewModel([
            'albums' => $this->table->fetchAll(),
        ]);
    }
}
Module.php

namespace Application;

use Application\Model\PlatnosciTable;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\ModuleManager\Feature\ConfigProviderInterface;

class Module
{
    const VERSION = '3.0.2dev';

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

    public function getServiceConfig() {
        return [
            'factories' => [
                Model\PlatnosciTable::class => function($container) {
                    $tableGateway = $container->get(Model\PlatnosciTableGateway::class);
                    return new Model\PlatnosciTable($tableGateway);
                },
                Model\PlatnosciTableGateway::class => function ($container) {
                    $dbAdapter = $container->get(AdapterInterface::class);
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Platnosci());
                    return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                },
            ],
        ];
    }

    public function getControllerConfig() {
        return [
            'factories' => [
                Controller\IndexController::class => function($container) {
                    return new Controller\IndexController(
                        $container->get(Model\PlatnosciTable::class)
                    );
                },
            ],
        ];
    }
}
public function getControllerConfig() {
        return [
            'factories' => [
                Controller\IndexController::class => function($container) {
                    return new Controller\IndexController(
                        $container->get(Model\PlatnosciTable::class)
                    );
                },
            ],
        ];
    }
module.config.php

namespace Application;

use Application\Controller\IndexController;
use Application\Model\PlatnosciTable;
use Interop\Container\ContainerInterface;
use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;

return [
    'router' => [
        'routes' => [
            // Strona glowna
            'home' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'counter',
                    ],
                ],
            ],
            'work' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/work',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
            'cennik' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/cennik',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'cennik',
                    ],
                ],
            ],
            // Panel
            'panel' => [
                'type'    => Segment::class,
                'options' => [
                    'route'    => '/panel',
                    'defaults' => [
                        'controller' => Controller\PanelController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
            'test' => [
                'type'    => Segment::class,
                'options' => [
                    'route'    => '/panel/test',
                    'defaults' => [
                        'controller' => Controller\PanelController::class,
                        'action'     => 'test',
                    ],
                ],
            ],
        ],
    ],
    'controllers' => [
        'factories' => [
//            Controller\IndexController::class => function(ContainerInterface $serviceMaganer, $controller) {
//                $repository = $serviceMaganer->get(PlatnosciTable::class);
//                return new IndexController($repository);
//            }
// that up there is just a try, now is like this one below.
            Controller\PanelController::class => InvokableFactory::class,
        ],
    ],
    'view_manager' => [
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => [
            'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
            'layout/panel'           => __DIR__ . '/../view/layout/panel_layout.phtml',
            'layout/counter'           => __DIR__ . '/../view/layout/counter.phtml',
            'layout/pusty'           => __DIR__ . '/../view/layout/pusty.phtml',
            'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ],
        'template_path_stack' => [
            __DIR__ . '/../view',
        ],
    ],
];

构造函数中预期的TableGateway在声明时未使用正确类的
use
。默认情况下,它在
Application\Model
名称空间中查找它,而类应该是
Zend\Db\TableGateway\TableGateway

namespace Application\Model;

// Add this to point TableGateway in __construct()
use Zend\Db\TableGateway\TableGateway;

// ...

class PlatnosciTable
{
    protected $tableGateway;

    // Now it should use a correct hinting
    public function __construct(TableGateway $tableGateway) {
        $this->tableGateway = $tableGateway;
    }

    // ...
}

还有一件事,在module.config.php和module.php中有两个工厂

如果我们在Module.php中创建

namespace Application;

use Application\Model\PlatnosciTable;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\ModuleManager\Feature\ConfigProviderInterface;

class Module
{
    const VERSION = '3.0.2dev';

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

    public function getServiceConfig() {
        return [
            'factories' => [
                Model\PlatnosciTable::class => function($container) {
                    $tableGateway = $container->get(Model\PlatnosciTableGateway::class);
                    return new Model\PlatnosciTable($tableGateway);
                },
                Model\PlatnosciTableGateway::class => function ($container) {
                    $dbAdapter = $container->get(AdapterInterface::class);
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Platnosci());
                    return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                },
            ],
        ];
    }

    public function getControllerConfig() {
        return [
            'factories' => [
                Controller\IndexController::class => function($container) {
                    return new Controller\IndexController(
                        $container->get(Model\PlatnosciTable::class)
                    );
                },
            ],
        ];
    }
}
public function getControllerConfig() {
        return [
            'factories' => [
                Controller\IndexController::class => function($container) {
                    return new Controller\IndexController(
                        $container->get(Model\PlatnosciTable::class)
                    );
                },
            ],
        ];
    }
它不能像module.config.php那样位于module.config.php中

'controllers' => [
        'factories' => [
            Controller\IndexController::class => InvokableFactory::class,
        ],
    ],
因为它会覆盖

  • 在“PlatnosciTable.php”中,构造函数应为-
  • 公共功能构造(tableGateway接口$tableGateway) { $this->tableGateway=$tableGateway; }

  • 并从“module.config.php”文件中删除以下部分-
  • “控制器”=>[ “工厂”=>[ Controller\PanelController::class=>InvokableFactory::class, ],]


    哑缓存。。。。现在它的构造()必须是Application\Model\TableGateway的实例,Zend\Db\TableGateway\TableGateway的实例,
    PlatnosciTable.php
    -有
    TableGateway
    构造函数参数,没有
    use
    指令,它连接到了错误的文件命名空间。它应该是
    Zend\Db\TableGateway\TableGateway
    ,所以指向那里。谢谢!工作:)