Zend Framework 2 模块

3
我是一名Zend Framework 2的新手,几天前开始使用它。我最近三天一直在为模块结构而苦恼。我想要有两个模块:主模块和管理员模块。
我有一个application.config.php文件:
return array(
    'modules' => array(
        'main', 'Administrator',
    ),
    'module_listener_options' => array(
        'module_paths' => array(
            './module',
            './vendor',
        ),
        'config_glob_paths' => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
    ),
);

"administrator"模块的module.config.php文件:


return array(
    'router' => array(
        'routes' => array(
            'Administrator' => array(
                'type' => 'Literal',
                'options' => array(
                    'route' => '/administrator',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Administrator\Controller',
                        'controller' => 'Index',
                        'action' => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type' => 'Segment',
                        'options' => array(
                            'route' => '/[:controller[/:action]]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Administrator\Controller\Index' => 'Administrator\Controller\IndexController',
            'Administrator\Controller\Blogs' => 'Administrator\Controller\BlogsController',
            'Administrator\Controller\Design' => 'Administrator\Controller\DesignController',
            'Administrator\Controller\FAQ' => 'Administrator\Controller\FAQController',
            'Administrator\Controller\Interests' => 'Administrator\Controller\InterestsController',
            'Administrator\Controller\Main' => 'Administrator\Controller\MainController',
            'Administrator\Controller\Pages' => 'Administrator\Controller\PagesController',
            'Administrator\Controller\RSS' => 'Administrator\Controller\RSSController',
            'Administrator\Controller\Users' => 'Administrator\Controller\UsersController'
        ),
    ),
    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions' => true,
        'doctype' => 'HTML5',
        'not_found_template' => 'error/404',
        'exception_template' => 'error/index',
        'template_map' => array(
            'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
            'administ/index/index' => __DIR__ . '/../view/administrator/index/index.phtml',
            'error/404' => __DIR__ . '/../view/error/404.phtml',
            'error/index' => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
);

还有“main”模块的module.config.php:

return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Literal',
                'options' => array(
                    'route' => '/',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Main\Controller',
                        'controller' => 'Index',
                        'action' => 'index',
                    ),
                ),
            ),
            'Main' => array(
                'type' => 'Literal',
                'options' => array(
                    'route' => '/main',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Main\Controller',
                        'controller' => 'Index',
                        'action' => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type' => 'Segment',
                        'options' => array(
                            'route' => '/[:controller[/:action]]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ),
                            'defaults' => array(
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Main\Controller\Index' => 'Main\Controller\IndexController'
        ),
    ),
    'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions' => true,
        'doctype' => 'HTML5',
        'not_found_template' => 'error/404',
        'exception_template' => 'error/index',
        'template_map' => array(
            'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
            'main/index/index' => __DIR__ . '/../view/main/index/index.phtml',
            'error/404' => __DIR__ . '/../view/error/404.phtml',
            'error/index' => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
);

这是我的文件结构:structure

我稍微编辑了一下此帖。现在,问题似乎只与布局有关。无论我打开什么页面都显示“管理员”模块的布局,以及正确的页面内容(或错误消息,这取决于控制器/模块/操作是否存在)。因此,问题似乎只与布局有关。

附注:当我在application.config.php中将管理员模块列为第一个时:

'modules' => array(
    'Administrator', 'Main'
),

它仅显示“主”模块布局,反之亦然 - 当“Main”是模块数组中的第一个元素时,在所有地方都显示管理员布局。

https://github.com/EvanDotPro/EdpModuleLayouts - Xerkus
模块的命名规范是使用您的前缀和名称组合。例如,EdpModuleLayouts:Edp是前缀,ModuleLayouts是名称。 - Xerkus
1个回答

1
'view_manager' => array(
    'display_not_found_reason' => true,
    'display_exceptions' => true,
    'doctype' => 'HTML5',
    'not_found_template' => 'error/404',
    'exception_template' => 'error/index',
    'template_map' => array(

        // The following key will be overriden, and the last loaded module config
        // is the one used, just comment it (for both modules config) the default 
        // behavior will pick-up the default/conventional layout.

        //'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',

        'administ/index/index' => __DIR__ . '/../view/administrator/index/index.phtml',
        'error/404' => __DIR__ . '/../view/error/404.phtml',
        'error/index' => __DIR__ . '/../view/error/index.phtml',
    ),
    'template_path_stack' => array(
        __DIR__ . '/../view',
    ),
),

那么我该如何使用特定于模块的布局? - Георги Банков
我尝试在config/autoload/global.php中添加module_layouts数组,并在模块特定配置中注释设置,但迄今为止没有成功。 - Георги Банков
请确保在您的Composer配置中添加EdpModule依赖项,运行“php composer.phar update”,并在您的配置中启用该模块。 参见:https://github.com/EvanDotPro/EdpModuleLayouts - yechabbi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接