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

Layout 如何为ZEND 2中的新模块设置新布局?,layout,module,zend-framework2,Layout,Module,Zend Framework2,有人能帮我设置管理模块的布局和应用程序模块的布局吗。在下图中,您可以看到我的文件夹结构: 这是管理模块中my module.config.php的内容: <?php return array( 'controllers' => array( 'invokables' => array( 'Administration\Controller\Admin' => 'Administration\Controller\

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

这是管理模块中my 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',
这是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');

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


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

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


根据要求,这里是我的方式为不同的布局。这不是我的想法,但由于我找不到源代码,我将在这里发布代码。如果有人知道,请在评论中添加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-mymodulename.phtml代替layout.phtml。它与前面突出显示的点一起工作得更好。

但是我必须在我想要使用的每个控制器中写入$this->布局?您可以创建一个
BaseAdministrationController
,并在
onDispatch()
方法中应用@Aneeq的解决方案。从基本控制器扩展所有与管理相关的控制器将是一个很好的做法。你真的尝试过吗?我已经有了一个composer.json文件,用这个覆盖它明智吗?它不需要你覆盖你的conposer.json文件。您可以通过运行
composer require evandotpro/edp module layouts:~1.0
(如果您使用的是Zendskeleton应用程序,请改用
php composer.phar require…
)。安装后,通过将
EdpModuleLayouts
添加到
application.config.php
中的模块列表来激活它。好的,它可以工作,但我不明白为什么我要在module.config.php的“template_map”=>数组中声明这样的布局(//'layout/layout'=>DIR./../view/layout/layout.phtml','application/index/index'=>DIR./../view/application/index/index.phtml','error/404'=>DIR./../view/error/index.phtml','error/index'=>DIR./../view/error/index.phtml',)我真的不明白这个问题,但无论如何,我注意到你有一个注释掉的行(数组(//'layout/layout')-这可能是原因吗?
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);
}
> > 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);
>       
>       
>       
>     }
'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',))