【问题标题】:Module (album) could not be initialized zf2 tutorial模块(专辑)无法初始化 zf2 教程
【发布时间】:2017-05-02 19:24:24
【问题描述】:

我正在完成 zend 框架 2 教程应用程序。我已将我的专辑模块目录设置如下:

我在启动 MAMP 服务器时遇到错误,Fatal error: Uncaught exception 'Zend\ModuleManager\Exception\RuntimeException' with message 'Module (Album) could not be initialized.'

如果我从以下代码中注释掉Album 模块(在/config/application.config.php 中):

'modules' => array(
    'Application',
    'Album',
),

我进入了骨架应用程序主页。 这是我的/module/Album/Module.php 代码:

namespace Album;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Album\Model\Album;
use Album\Model\AlbumTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;

class Module implements AutoloaderProviderInterface, ConfigProviderInterface {

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’; 
}

public function getServiceConfig() {
    return array(
        ’factories’ => array(
            ’Album\Model\AlbumTable’ => function($sm) { 
                $tableGateway = $sm->get(’AlbumTableGateway’); 
                $table = new AlbumTable($tableGateway);
                return $table;
            },
            ’AlbumTableGateway’ => function ($sm) {
                $dbAdapter = $sm->get(’Zend\Db\Adapter\Adapter’);
                $resultSetPrototype = new ResultSet(); $resultSetPrototype->setArrayObjectPrototype(new Album());
                return new TableGateway(’album’, $dbAdapter, null, $resultSetPrototype);
            }, 
        ),
    ); 
}
}

这是我在/module/Album/config/ 中的module.config.php 代码:

return array(
’controllers’ => array(
    ’invokables’ => array(
        ’Album\Controller\Album’ => ’Album\Controller\AlbumController’,
    ),
),

’view_manager’ => array( 
    ’template_path_stack’ => array(
        ’album’ => __DIR__ . ’/../view’,
    ),
),

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

我已经阅读了一些遇到类似情况的人,但他们的问题是由于拼写错误/错误命名的类。我看不出我的代码有什么问题(甚至直接从教程中复制/粘贴)。

谁能给我一些建议?

谢谢

【问题讨论】:

  • 尝试更新您的composer 文件。
  • @KunalDethe 在作曲家文件中添加内容或运行作曲家更新命令?
  • 我创建了一个新的骨架应用程序再次尝试此操作,但我遇到了同样的问题。教程有问题吗?我会很惊讶,但我真的不知道还能是什么
  • 尝试通过来自https://github.com/zendframework/ZendSkeletonApplicationgit clone 和来自framework.zend.com/manual/2.3/en/user-guide/…composer install/update 安装骨架
  • @KunalDethe 刚刚开始尝试这个。不幸的是,我遇到了同样的问题。

标签: php zend-framework2


【解决方案1】:

我也遇到了同样的问题,解决办法是用<?php开始每个.php文件 这在教程中并不清楚(如果您只是从那里复制代码),这就是我遇到相同异常的原因。

【讨论】:

    【解决方案2】:

    我能看到的唯一原因是您应该将所有 替换为普通单引号'。 使用 可能会导致意外行为。

    【讨论】:

    • 我认为这可能只是一个复制/粘贴问题。我把它们都换掉了,没有任何区别。
    • 我遇到有人说目录结构错误,但他们没有说明正确的结构应该是什么。上面的截图正是教程中的内容
    【解决方案3】:

    教程 module.config.php 文件中有错误。 更改'id' => '[0-9]+','id' => '[0-9]*', + 表示一位或多位数字。如果您只是调用http://zf2-tutorial.localhost/album/,则 url 中没有数字,因此 rewriterule 不匹配。从 + 更改为 *(0 或更多)

    【讨论】:

      【解决方案4】:

      我遇到了完全相同的问题,我的问题和解决方案是

      教程中的第一个命令是:

      php composer.phar create-project --stability="dev" zendframework/skeleton-application path/to/install
      

      它被切断了我的屏幕,所以我将命令复制/粘贴到命令行而不阅读结尾。所以我的应用程序目录结构包括一个目录 ./path/to/install ,所有安装文件都在其中,包括 application.config.php。

      将 /path/to/install/ 中的所有内容移动到应用程序的根目录后,Zend 就可以找到相册模块。

      注意:/path/to/install/module 包含专辑模块,但如果这样做会失败

      mv ./path/to/install/* .
      

      所以请务必将 /path/to/install/module 中的应用程序模块移动到 app_root_dir/module/ 中。

      【讨论】:

        【解决方案5】:

        您可以查看 composer.json 天气包括以下内容:

        "autoload": {
            "psr-4": {
                "Application\\": "module/Application/src/",
                "Album\\": "module/Album/src"
            }
         },
        

        【讨论】:

          【解决方案6】:

          请检查您的 Module::getConfig() , 和玩: return include __DIR__ . '/../config/module.config.php'; 我添加了 '../' 前缀,效果很好。

          【讨论】:

            猜你喜欢
            • 2012-11-08
            • 2016-01-02
            • 2022-07-03
            • 2013-09-03
            • 1970-01-01
            • 1970-01-01
            • 2012-08-22
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多