基于 ZF2 模块的布局


ZF2 Module based Layouts

我正在尝试学习Zend Framework 2,目前面临一个问题。

我创建了一个名为"Admin"的模块,并为管理模块定义了布局。现在的问题是应用程序模块也在加载管理模块的布局。如果我浏览管理员或应用程序模块,则正在加载相同的布局。我已经通过谷歌搜索尝试了多种解决方案,但没有让任何人工作。

我通过复制应用程序模块目录并将其重命名为管理员来创建管理模块,并在子目录名称和代码文件中将"应用程序"更改为"管理员"。

这是

Khalids帖子的视觉呈现,在config/application.config.php文件中又增加了一个第 1 步。第 2 步。将应用程序模块复制到并重命名为管理员

//config/application.config.php
    'modules' => array(
            'Application'
            ,'Test', // add this line
        ),


<?php
//Step 3.
//

你的Test/config/module.config.php应该看起来像这样

return array(
    'router' => array(
        'routes' => array(
            /* 'home' => array(
                'type' => 'Zend'Mvc'Router'Http'Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                       // 'controller' => 'StickyNotes'Controller'Album', 
                       //'controller' => 'Application'Controller'Index',
                        'action'     => 'index',
                    ),
                ),
            ), */
            // The following is a route to simplify getting started creating
            // new controllers and actions without needing to create a new
            // module. Simply drop new controllers in, and you can access them
            // using the path /application/:controller/:action
            'application' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/test',
                    'defaults' => array(
                        '__NAMESPACE__' => 'Test'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',
        ),
    ),
    'translator' => array(
        'locale' => 'en_US',
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Test'Controller'Index' => 'Test'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/test'           => __DIR__ . '/../view/layout/test.phtml',
            'application/index/index' => __DIR__ . '/../view/test/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
);

第 4 步。最后,您的测试/模块文件应如下所示:->

namespace Test;
use Zend'Mvc'ModuleRouteListener;
use Zend'Mvc'MvcEvent;
class Module
{
    public function onBootstrap(MvcEvent $e)
    {
        $e->getApplication()->getServiceManager()->get('translator');
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
    }
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }
    public function getAutoloaderConfig()
    {
        return array(
            'Zend'Loader'StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
}

您将准备好滚动。

好吧,我终于找到了解决方案。

假设您要创建一个名为"Admin"的模块,步骤如下:

1-复制应用程序模块目录并将其重命名为"管理员"。它是模块的名称。

2-更新管理模块中最初指向应用程序模块的所有引用。(将"应用程序"更改为"应用程序","应用程序"更改为"管理员")

3-在Admin/config/module.config中.php删除"home"路由。

4-更新您的布局和视图。

5-使用 http://example.com/admin 在浏览器中进行测试

就是这样。

你不需要任何外部布局库,如"EdpModuleLayouts"

干杯:)