【问题标题】:Different layout for different modules不同模块的不同布局
【发布时间】:2016-10-13 18:29:40
【问题描述】:

发生了一些非常奇怪的事情。我有两个模块,一个叫Application,另一个叫Dashboard,它们是不同的,彼此无关。我想对它们中的每一个都使用 phtml 布局,这就是我所做的:

module/Application/config/module.config.php:

// ...

'view_manager' => [
    'display_not_found_reason' => true,
    'display_exceptions'       => true,
    'doctype'                  => 'HTML5',
    'not_found_template'       => 'error/404',
    'exception_template'       => 'error/index',
    'template_map' => [
        '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' => [
        __DIR__ . '/../view',
    ],
],

module/Dashboard/config/module.config.php:

// ...

'view_manager' => [
    'doctype'  => 'HTML5',
    'template_map' => [
        'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml',
        'dashboard/index/index'   => __DIR__ . '/../view/dashboard/index/index.phtml',
        'error/404'               => __DIR__ . '/../view/error/404.phtml',
        'error/index'             => __DIR__ . '/../view/error/index.phtml',
    ],
    'template_path_stack' => [
        __DIR__ . '/../view',
    ],
],

我创建了两个独立的布局,一个在module/Application/view/layout/layout.phtml,另一个在module/Dashboard/view/layout/layout.phtml,逻辑上它必须工作,但它没有,它总是调用Dashboard布局,即使是Application . 我想知道,如何为每个模块使用单独的布局?

【问题讨论】:

标签: php zend-framework zend-framework2


【解决方案1】:

我在之前的 ZF2 项目中遇到了同样的问题。问题是您对两个模块使用相同的“布局/布局”标识符,并且在配置合并期间,一个会丢失。

这个想法是为标识符提供不同的名称,并使用一个允许更改布局的抽象控制器。在dispatch 事件中,您附加了一个函数,它将为您的模块设置布局:

Module.php(您的主模块)

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, '\\'));
    $controller->layout($moduleNamespace . '/layout');
    }, 100);
}  

并且在 module.config.php 的所有模块中使用不同的布局(例如 Dashboard):

'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(
        'Dashboard/layout'           => __DIR__ . '/../view/layout/layout.phtml',
        'Dashboard/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',
    ),
),

应该没问题。否则你也可以使用其他方的代码,比如EdpModuleLayouts,但它不再被维护了......我的解决方案的好处是你应该明白你在做什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-11
    • 1970-01-01
    • 2015-10-09
    • 2011-10-03
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-21
    相关资源
    最近更新 更多