简体   繁体   中英

Cannot register module in ZF2

I have a custom module loaded in via composer in vendor/data-table.

Although I think I have everything set up correctly, I get the following error:

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

If I remove MJErwin\\DataTable from my application.config.php everything works fine, so it appears to be an issue with the module itself.

The structure is as follows:

模块结构

My 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',
        ],
    ],
];

Then in 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'
        ]
    ],
];

I see two possibilities there.

First, you installed module manually, in that case zf2 module loader will look in following paths: modules/MJErwin/DataTable/Module.php or vendor/MJErwin/DataTable/Module.php

Other possibility is you installed module via composer, but forgot to add Module class to composer.json autoload section

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM