如何在ZEND 2中为新模块设置新布局? [英] How do I set up a new layout to a new module in ZEND 2?

查看:69
本文介绍了如何在ZEND 2中为新模块设置新布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我为管理模块设置布局并为应用模块设置布局吗? .在下图中,您可以看到我的文件夹结构:

Can someone help me to set up a layout for the administration module and set up a layout for the application module ? . In the image below you can see my folder structure:

这是管理模块中我module.config.php的内容:

This is the content of my module.config.php from the administration module:

<?php 
 return array(
     'controllers' => array(
         'invokables' => array(
             'Administration\Controller\Admin' => 'Administration\Controller\AdminController',
         ),
     ),
     // The following section is new and should be added to your file
     'router' => array(
         'routes' => array(
             'album' => array(
                 'type'    => 'segment',
                 'options' => array(
                     'route'    => '/administration[/:action][/:id]',
                     'constraints' => array(
                         'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                         'id'     => '[0-9]+',
                     ),
                     'defaults' => array(
                         'controller' => 'Administration\Controller\Admin',
                         'action'     => 'index',
                     ),
                 ),
             ),
         ),
     ),
      'view_manager' => array(
        //'base_path' => 'http://www.attila-naghi.com/',
        '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/layout2.phtml',
          //  'administration/admin/index'     => __DIR__ . '/../view/administration/admin/index.phtml',
            // 'error/404'                      => __DIR__ . '/../view/error/404.phtml',
            // 'error/index'                    => __DIR__ . '/../view/error/index.phtml',

), 'template_path_stack'=>数组( DIR . '/../看法', ), ), ) ?> 在这里,我设置了布局.但是由于某些原因,如果我访问应用模块,它将从管理模块加载布局.为什么 ? 这是应用程序模块中的module.config.php文件的内容:

), 'template_path_stack' => array( DIR . '/../view', ), ), ) ?> here i set up the layout. But for some reason if i access the application module, it loads the layout from the administration module. WHY ? this is the content of the module.config.php file from the application module:

return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Zend\Mvc\Router\Http\Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action'     => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type' => 'Segment',
                        'options' => array(
                            'route' => '[:controller[/:action]][/:param1]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action' => '[a-zA-Z][a-zA-Z0-9_-]*'
                            ),
                            'defaults' => array(
                                'action' => 'index',
                                '__NAMESPACE__' => 'Application\Controller',
                               // 'param1' => 'tralala'
                            )
                        )
                    )
                )
            ),
        ),
    ),

    'service_manager' => array(
        'abstract_factories' => array(
            'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
            'Zend\Log\LoggerAbstractServiceFactory',
        ),
        'aliases' => array(
            'translator' => 'MvcTranslator',
        ),
    ),
    'translator' => array(
        'locale' => 'en_US',
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Application\Controller\Index'          => 'Application\Controller\IndexController',
            'Application\Controller\Create'         => 'Application\Controller\CreateController',
            'Application\Controller\Blog'           => 'Application\Controller\BlogController',
            'Application\Controller\Portofolio'     => 'Application\Controller\PortofolioController',
            'Application\Controller\User'           => 'Application\Controller\UserController',
        ),
    ),
    'view_manager' => array(
        'base_path' => 'http://www.attila-naghi.com/',
        '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',
            'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
    // Placeholder for console routes
    'console' => array(
        'router' => array(
            'routes' => array(
            ),
        ),
    ),
);

这是application.config.file的内容:

and this is the application.config.file content:

 return array(
    // This should be an array of module namespaces used in the application.
    'modules' => array(
        'Application',
        'Administration'
    ), 
......

推荐答案

我最近找到了一种方法(其他人的解决方案). 将此添加到您的module.config.php(在我的情况下,该模块称为Album,它基于ZF2的演示应用程序):

I have recently found a way to do it (someone else's solution). Add this to your module.config.php (in my case the module is called Album, this is based on the demo application of ZF2):

'module_layouts' => array(
    'Album' => 'layout/layout.phtml'
),

需要在主Module.php文件中进行其他必要的更改(在此添加此onBootstrap方法并根据需要进行编辑):

The other necessary change needs to be done in the main Module.php file (add this onBootstrap method there and edit as may be needed):

public function onBootstrap($e) {
    $e->getApplication()
            ->getEventManager()
            ->getSharedManager()
            ->attach('Zend\Mvc\Controller\AbstractController', 'dispatch', 
                    function($e) {
                        $controller = $e->getTarget();
                        $controllerClass = get_class($controller);
                        $moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
                        $config = $e->getApplication()->getServiceManager()->get('config');
                        if (isset($config['module_layouts'][$moduleNamespace])) {
                            $controller->layout($config['module_layouts'][$moduleNamespace]);
                        }
                    }, 100);
}

这篇关于如何在ZEND 2中为新模块设置新布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆