我正在尝试开始使用ZF2,在编写教程中的代码时遇到了问题(在ZF网站上)。我的代码:
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',
)
),
);
问题是:
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
为什么在启动时显示?(在我的项目中:/var/www/zend2/)。如果我从application.config.php
中删除模块声明,它可以正常工作。我的问题是什么?:/
哎哟,解决了
在Module.php
中,类必须命名为Module
,而不是自己的名称。。。
当PSR-4加载出现此问题时,您还可以检查模块名称和文件夹路径是否正确,以便在composer.json
文件中自动加载:
"autoload": {
"psr-4": {
"YourModule''": "module/YourModule/src/",
... other modules ...
}
},
然后在修复后运行:
composer update
我认为您的类索引已经过时,您可以使用以下命令:
composer install -o