【问题标题】:How do I set up a new layout to a new module in ZEND 2?如何在 ZEND 2 中为新模块设置新布局?
【发布时间】:2015-03-24 02:05:00
【问题描述】:

有人可以帮我设置 administration 模块的布局并设置 application 模块的布局吗? .在下图中你可以看到我的文件夹结构:

这是管理模块中我的 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' => 数组( 目录。 '/../看法', ), ), ) ?> 在这里我设置了布局。但由于某种原因,如果我访问 application 模块,它会从 administration 模块加载布局。为什么 ? 这是 application 模块中 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'
    ), 
......

【问题讨论】:

    标签: layout module zend-framework2


    【解决方案1】:

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

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

    【讨论】:

    • 但我必须在我想使用的每个控制器中编写 $this->layout ?
    • 您可以创建一个BaseAdministrationController 并在其中应用@Aneeq 的解决方案onDispatch() 方法。从基本控制器扩展所有与管理相关的控制器将是一种很好的做法。
    【解决方案2】:

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

    https://github.com/EvanDotPro/EdpModuleLayouts

    【讨论】:

    • 你真的试过了吗?我已经有一个 composer.json 文件,用这个覆盖它是否明智?
    • 它不需要你覆盖你的 conposer.json 文件。您可以通过运行composer require evandotpro/edp-module-layouts:~1.0 将该模块安装到您的应用程序中(如果您使用的是 ZendSkeletonApplication,请改用php composer.phar require ...)。安装后,您可以通过将 EdpModuleLayouts 添加到 application.config.php 中的模块列表来激活它。
    【解决方案3】:

    我最近找到了一种方法(其他人的解决方案)。 将此添加到您的 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);
    }
    

    【讨论】:

    • 好的,它可以工作,但是我不明白为什么我在 module.config.php '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', ), ______________为什么在两个模块中都不起作用
    • 我真的不明白这个问题,但无论如何,我注意到你有一个注释掉的行( array( // 'layout/layout' ) - 这可能是原因吗?
    【解决方案4】:

    根据这里的要求,这是我针对不同布局的方式。这不是我的想法,但由于我找不到源代码,所以我将在这里发布代码。如果有人知道,请在评论中添加 URL,我会将其包含在答案中。如果您阅读了我在另一个问题中给您的资料以及上述答案,我肯定会理解这里发生的事情。

    模块.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',))
    

    【讨论】:

      【解决方案5】:

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

      【讨论】:

        猜你喜欢
        • 2013-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-22
        相关资源
        最近更新 更多