Php 传递给控制器\foo的参数1必须是控制器\foo的实例
我对Zend框架非常陌生。我跟随固体Zend教程网站(从A到Z)…我被困在这里…请帮助我 一些解决方案并没有多大帮助。只做了一些事情,但什么也没做。同下面的帖子 击中这个错误Php 传递给控制器\foo的参数1必须是控制器\foo的实例,php,zend-framework,Php,Zend Framework,我对Zend框架非常陌生。我跟随固体Zend教程网站(从A到Z)…我被困在这里…请帮助我 一些解决方案并没有多大帮助。只做了一些事情,但什么也没做。同下面的帖子 击中这个错误 Argument 1 passed to Album\Controller\AlbumController::__construct() must be an instance of Album\Controller\AlbumTable, instance of Album\Model\AlbumTable given
Argument 1 passed to Album\Controller\AlbumController::__construct() must be an instance of Album\Controller\AlbumTable, instance of Album\Model\AlbumTable given, called in C:\xampp\htdocs\zendF\module\Album\src\Module.php on line 43
堆栈跟踪
#0 C:\xampp\htdocs\zendF\module\Album\src\Module.php(43): Album\Controller\AlbumController->__construct(Object(Album\Model\AlbumTable))
#1 C:\xampp\htdocs\zendF\vendor\zendframework\zend-servicemanager\src\ServiceManager.php(758): Album\Module->Album\{closure}(Object(Zend\ServiceManager\ServiceManager), 'Album\\Controlle...', NULL)
#2 C:\xampp\htdocs\zendF\vendor\zendframework\zend-servicemanager\src\ServiceManager.php(200): Zend\ServiceManager\ServiceManager->doCreate('Album\\Controlle...')
#3 C:\xampp\htdocs\zendF\vendor\zendframework\zend-servicemanager\src\AbstractPluginManager.php(141): Zend\ServiceManager\ServiceManager->get('Album\\Controlle...')
#4 C:\xampp\htdocs\zendF\vendor\zendframework\zend-mvc\src\DispatchListener.php(95): Zend\ServiceManager\AbstractPluginManager->get('Album\\Controlle...')
#5 C:\xampp\htdocs\zendF\vendor\zendframework\zend-eventmanager\src\EventManager.php(322): Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#6 C:\xampp\htdocs\zendF\vendor\zendframework\zend-eventmanager\src\EventManager.php(179): Zend\EventManager\EventManager->triggerListeners(Object(Zend\Mvc\MvcEvent), Object(Closure))
#7 C:\xampp\htdocs\zendF\vendor\zendframework\zend-mvc\src\Application.php(332): Zend\EventManager\EventManager->triggerEventUntil(Object(Closure), Object(Zend\Mvc\MvcEvent))
#8 C:\xampp\htdocs\zendF\public\index.php(40): Zend\Mvc\Application->run()
#9 {main}
mymodule.php
<?php
namespace Album;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
class Module implements ConfigProviderInterface
{
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 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)
);
},
],
];
}
}
?>
my AlbumController.php
<?php
namespace Album\Controller;
use Zend\Mvc\Controller\AbstractActionController;
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() {
}
}
?>
my Module.config.php
<?php
namespace Album;
use Zend\Router\Http\Segment;
return [
'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',
],
],
];
?>
您应该将依赖项放在名称空间之后。
试试这个:
<?php
namespace Album\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Album\Model\AlbumTable;
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() {
}
}
您的控制器混淆了您试图在构造函数中注入的依赖项。要解决此问题,您应该在类之前设置“use”声明,如use Album\Model\AlbumTable
ty非常感谢@fmsthird为您提供反馈..我建议use Album\Model\AlbumTable
inside AlbumController.php…但这次遇到了此错误。Class'Album\Controller\ViewModel'未找到。
。抱歉..我是ZendIt的新手,没问题,我发布了一个答案,你可以检查一下。作为提示,我想你可能正在为ZF 2做教程。ZF3是几年前发布的,其文档和教程可在此处找到:。将ServiceManager注入控制器是不应该再做的事情。兄弟..像符咒一样工作!!我犯的一些错误。global.php'dsn'=>sprintf('sqlite:%s/data/zendF.db',realpath(getcwd()),
(确保数据库名称相同)2。添加使用相册\模型\相册表代码>内部控制器