ZF2: Module could not be initialized

2019-06-22 07:27发布

问题:

I'm trying to get started with ZF2 and I have a problem when I writting code from tutorial (on ZF website). My code:

Module.php:
<?php
namespace About;

class About
{
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

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

config/module.config.php:

<?php
return array(
    'controllers' => array(
        'invokables' => array(
            'About\Controller\About' => 'About\Controller\AboutController',
        ),
    ),

    'router' => array(
        'routes' => array(
            'album' => array(
                'type'      =>  'segment',
                'options'   => array(
                    'route'     => '/about[/:action][/:id]',
                    'constraints' => array(
                        'action'    =>  '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'        =>  '[0-9]+',
                    ),
                    'defaults'  => array(
                        'controller'    => 'About\Controller\About',
                        'action'        =>  'index',
                    ),
                ),
            ),
        ),
    ),

    'view_manager' => array(
        'template_path_stack' => array(
            'about' => __DIR__ . '/../view',
        )
    ),
);

Problem is:

Fatal error: Uncaught exception 'Zend\ModuleManager\Exception\RuntimeException' with message 'Module (About) could not be initialized.' in /var/www/zend2/vendor/zendframework/zendframework/library/Zend/ModuleManager/ModuleManager.php on line 175

Why it's shown on start? (in my project: /var/www/zend2/). If I remove module declaration from application.config.php it works okay. What is my problem? :/

回答1:

Ouch, solved!
In Module.php class must be named Module, not own name...



回答2:

When having this issue with PSR-4 loading you could also check whether the module name and path to the folder are correct for autoloading inside your composer.json file:

"autoload": {
  "psr-4": {
    "YourModule\\": "module/YourModule/src/",
    ... other modules ...
  }
},

And then after fixing things run:

composer update