ZF2: Module could not be initialized

你说的曾经没有我的故事 提交于 2019-12-23 09:47:40

问题


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



回答3:


I believe your class indexes are outdated, you can follow this command:

    composer install -o


来源:https://stackoverflow.com/questions/17265447/zf2-module-could-not-be-initialized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!