【问题标题】:Set a generic layout for all modules in Zend framework 2为 Zend 框架 2 中的所有模块设置通用布局
【发布时间】:2013-05-19 20:20:39
【问题描述】:

我正在做一个 ZF2 项目,我的目录中有一些模块:

/module/module1
/module/module2
/module/module3
/module/module4
[...]

但是,在每个模块中,我也分别有一个特定的布局:

/module/module1/view/layout/layout.phtml
/module/module2/view/layout/layout.phtml
/module/module3/view/layout/layout.phtml
/module/module4/view/layout/layout.phtml

我的问题是:如何为我的所有模块设置一个通用布局,而无需在需要时修改每个布局。

谢谢

【问题讨论】:

    标签: php layout zend-framework2


    【解决方案1】:

    您可以在每个模块配置中将布局设置为您想要的任何内容,只需将布局更改为您想要的任何内容:

    module.config.php 或内部 getConfig()

    'view_manager' => array(
        // other stuff here.. 
        'template_map' => array(
            // use Applications layout instead
            'layout/layout' => __DIR__ . '/../Application/view/application/layout/layout.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
    

    或者您可以设置每个模块以选择性地在 Module.php 中设置其布局:

    模块.php

    /**
     * Initialize
     */
    public function init(ModuleManager $manager)
    {
        $events = $manager->getEventManager();
        $sharedEvents = $events->getSharedManager();
        $sharedEvents->attach(__NAMESPACE__, 'dispatch', function($e) {
            /* @var $e \Zend\Mvc\MvcEvent */
            // fired when an ActionController under the namespace is dispatched.
            $controller = $e->getTarget();
            $routeMatch = $e->getRouteMatch();
            /* @var $routeMatch \Zend\Mvc\Router\RouteMatch */
            $routeName = $routeMatch->getMatchedRouteName();
            $controller->layout('application/layout/layout');
        }, 100);
    }
    

    【讨论】:

    • 这是一个很大的帮助......如果我只需要为每个模块实现不同的导航和 css 怎么办?现在我用局部实现导航...
    • 尊敬的专家,如果我有不同名称的子模块,您的第二个解决方案不会触发,因为命名空间不同。如果任何模块有多个子模块。我们如何为所有子模块设置布局。提前致谢
    猜你喜欢
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    相关资源
    最近更新 更多