首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复ZF3 __Construct ArgumentCountError

如何修复ZF3 __Construct ArgumentCountError
EN

Stack Overflow用户
提问于 2019-11-12 19:40:07
回答 1查看 190关注 0票数 0

我正在创建一个CRUD应用程序示例,其中需要将我的ZF3应用程序连接到数据库

我的模型分别是Post.php和PostTable.php

代码语言:javascript
复制
namespace Post\Model;

    /**
     * 
     */
    class Post
    {

        protected $id;
        protected $title;
        protected $description;
        protected $category;
        public function exchangeArray($data){
            $this->id = $data['id'];
            $this->title = $data['title'];
            $this->description = $data['description'];
            $this->category = $data['category'];
        }
        public function getId(){
            return $this->id;
        }
        public function getTitle(){
            return $this->titile;
        }
        public function getDescription(){
            return $this->description;
        }
        public function getCategory(){
            return $this->category;
        }
    }

namespace Post\Model;

    use Zend\Db\TableGateway\TableGatewayInterface;
    /**
     * 
     */
    class PostTable
    {

        function __construct(TableGatewayInterface $tableGateway)
        {
            # code...
            $this->tableGateway = $tableGateway;

        }

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

    } 

Module.config.php

代码语言:javascript
复制
namespace Post;

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

return [
    'router' => [
        'routes' => [
            'home' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
            'application' => [
                'type'    => Segment::class,
                'options' => [
                    'route'    => '/post[/:action]',
                    'defaults' => [
                        'controller' => Controller\IndexController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
        ],
    ],
    'controllers' => [
        'factories' => [
            Controller\IndexController::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',
            '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',
        ],
    ],
];

我的Module.php

代码语言:javascript
复制
namespace Post;
use Zend\Db\AdapterInterface;
use Zend\Db\TableGateway;
use Zend\Db\ResultSet\ResultSet;
use Zend\ModuleManager\Feature\ConfigProviderInterface;

class Module implements ConfigProviderInterface
{
    const VERSION = '3.0.3-dev';

    public function getConfig()
    {
        return include __DIR__ . '/../config/module.config.php';
    }
    public function getServiceConfig(){
        return [
            'factories' => [
                Model\PostTable::class => function($container){
                    $tableGateway = $container->get(Model\PostTableGateway::class);
                    return new Model\PostTable($tableGateway);
                },
                Model\PostTableGateway::class => function($container){
                    $adapter = $container->get(AdapterInterface::class);
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Model\Post);
                    return new tableGateway('post',$adapter,null,$resultSetPrototype);
                }
            ],

        ];
    }

    public function getControllerConfig(){
        return [
            'factories' => [
                controller\IndexController::class => function($container){
                    return new Controller\IndexController($controller->get(Model\PostTable::class));

                }
            ]
        ];
    }
}

IndexController.php

代码语言:javascript
复制
namespace Post\Controller;

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

class IndexController extends AbstractActionController
{
    public function __construct($table){
        $this->table = $table;
    }
    public function indexAction()
    {
        return new ViewModel();
    }
}

在第30行的F:\Path\to\Project\vendor\zendframework\zend-servicemanager\src\Factory\InvokableFactory.php中传递的函数Post\Controller\IndexController::__construct(),0的参数太少,而第15行的F:\Path\to\Project\module\Post\src\Controller\IndexController.php:15恰好为1,这是我的ArgumentCountError异常

代码语言:javascript
复制
public function __construct($table){
        $this->table = $table;
    }

我希望从数据库中获得结果集。但是构造函数会输出一个参数计数错误异常

EN

回答 1

Stack Overflow用户

发布于 2019-11-12 20:37:29

您的代码中有两个错误。

第一个是在module.config.php中,在键controllers下,必须删除旧的IndexController定义:

代码语言:javascript
复制
'router' => [
    ...
],
'controllers' => [
    'factories' => [
        // This one is obsolete, and you must remove it.
        // Controller\IndexController::class => InvokableFactory::class,
    ],
],
'view_manager' => [
    ...
],

由于您在Module.php类中声明了控制器,因此module.config.php中的controllers键不再是必需的,您可以将其删除:

代码语言:javascript
复制
'router' => [
    ...
],
'view_manager' => [
    ...
],

第二个错误:在IndexController工厂中(在Module.php中)有一个(双重)错误。

第一个是输入错误的命名空间,而不是Controller.

  • The。第二个是使用$controller而不是$container来检索PostTable实例。

代码语言:javascript
复制
public function getControllerConfig() {
    return [
        'factories' => [
            Controller\IndexController::class => function($container) {
                return new Controller\IndexController($container->get(Model\PostTable::class));
            }
        ]
    ];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58817971

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档