无法在 ZF2 中注册模块

Cannot register module in ZF2

我在 vendor/data-table.

中通过 composer 加载了一个自定义模块

虽然我认为我已经正确设置了所有内容,但我收到以下错误:

PHP Fatal error: Uncaught exception 'Zend\ModuleManager\Exception\RuntimeException' with message 'Module (MJErwin\DataTable) could not be initialized.' in /Applications/MAMP/htdocs/rota/vendor/zendframework/zendframework/library/Zend/ModuleManager/ModuleManager.php:189

如果我从 application.config.php 中删除 MJErwin\DataTable 一切正常,所以这似乎是模块本身的问题。

结构如下:

我的Module.php:

namespace MJErwin\DataTable;

use Zend\ModuleManager\Feature\ConfigProviderInterface;

class Module implements ConfigProviderInterface
{

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

    public function getAutoloaderConfig()
    {
        return [
            'Zend\Loader\StandardAutoloader' => [
                'namespaces' => [
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ],
            ],
        ];
    }
}

module.config.php:

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

然后在 application.config.php:

<?php
/**
 * Configuration file generated by ZFTool
 * The previous configuration file is stored in application.config.old
 *
 * @see https://github.com/zendframework/ZFTool
// */
return [
    'modules' => [
        'Application',
        'MJErwin\DataTable',
        'DoctrineModule',
        'DoctrineORMModule',
        'Environment',
        'ZendDeveloperTools',
        'ZfcTwig',
        'ZfcBase',
        'ZfcUser',
        'ZfcUserDoctrineORM',
        'Navigation'
    ],
    'module_listener_options' => [
        'module_paths' => [
            './module',
            './vendor'
        ],
        'config_glob_paths' => [
            'config/autoload/{,*.}{global,local}.php'
        ]
    ],
];

我看到了两种可能性。

首先,您手动安装了模块,在这种情况下,zf2 模块加载器将查找以下路径:modules/MJErwin/DataTable/Module.phpvendor/MJErwin/DataTable/Module.php

另一种可能是您通过 composer 安装了模块,但忘记将模块 class 添加到 composer.json 自动加载部分

"autoload": {
    "psr-0": {
        "MJErwin\DataTable": "src/"
    },
    "classmap": [
        "./Module.php"
    ]
}