繁体   English   中英

如何在ZEND 2中为新模块设置新布局?

[英]How do I set up a new layout to a new module in ZEND 2?

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

在此输入图像描述

这是来自管理模块的module.config.php的内容:

<?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'=> array( DIR 。'/ / / view',),),)?>这里我设置了布局。 但出于某种原因,如果我访问应用程序模块,它会从管理模块加载布局。 为什么? 这是应用程序模块中module.config.php文件的内容:

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内容:

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

您必须从控制器更改布局。 只需在ViewModel上方指定此代码即可

$this->layout('administration/admin/index');

所有模块的配置合并为单个配置。 最后加载的模块将覆盖第一个模块的布局。 您可以使用下面的模块设置每个模块的布局。

https://github.com/EvanDotPro/EdpModuleLayouts

我最近找到了一种方法(别人的解决方案)。 将它添加到你的module.config.php(在我的例子中,模块被称为Album,这是基于ZF2的演示应用程序):

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

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

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

根据要求,我的方式是不同的布局。 这不是我的想法,但由于我找不到来源,我将在这里发布代码。 如果有人知道,请在评论中添加URL,我会将其包含在答案中。 如果您在另一个问题和上述答案中阅读了我给您的消息来源,我相信我会明白这里发生了什么。

Module.php

使用Zend \\ ModuleManager \\ ModuleManager;

> > public function init(ModuleManager $moduleManager)
>     {
>         $sharedEvents = $moduleManager->getEventManager()->getSharedManager();
>         $sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
>             // This event will only be fired when an ActionController under the MyModule namespace is dispatched.
>           
>             $controller = $e->getTarget();
              $controller->layout('backofficeLayout');
>             
>         }, 100);
>       
>       
>       
>     }

module.config.php

'view_manager' => array(
 'display_not_found_reason' => true,
 'display_exceptions'       => true,

    'doctype'                  => 'HTML5',
    'not_found_template'       => 'error/404',
    'exception_template'       => 'error/index', 
    'template_path_stack' => array(
        'backoffice' => __DIR__ . '/../view',
    ),
    'template_map' => array(

        'backofficeLayout'    => __DIR__ . '/../view/layout/myaccount-backoffice.phtml',))

我刚刚注意到,最好为布局文件夹中的每个布局使用自定义名称。 而不是使用layout.phtml使用layout-mymodulename.phtml。 它与之前突出的要点一起工作得更好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM