为Zend Framework 2中的所有模块设置通用布局 [英] Set a generic layout for all modules in Zend framework 2
问题描述
我正在开发ZF2项目,并且我的目录中有一些模块:
I'm working on a ZF2 Project, and I have some modules in my directories :
/module/module1
/module/module2
/module/module3
/module/module4
[...]
但是,在每个模块中,我也分别有一个特定的布局:
But, in each module I also have a specific layout, respectively :
/module/module1/view/layout/layout.phtml
/module/module2/view/layout/layout.phtml
/module/module3/view/layout/layout.phtml
/module/module4/view/layout/layout.phtml
我的问题是:我如何为我的所有模块设置通用布局,而不必在需要时修改每个布局.
My question is : How can I set a generic layout for all my modules without to have to modify each layout when I want.
谢谢
推荐答案
您可以在每个模块配置中将布局设置为所需的布局,只需将布局更改为所需的布局即可.
You can set the layout to be what ever you want in each modules config, just change the layout to be what ever you want:
module.config.php或在getConfig()内
module.config.php or inside 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中设置每个模块以有选择地设置其布局:
Or you can set each module to selectively set it's layout in Module.php:
Module.php
Module.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);
}
这篇关于为Zend Framework 2中的所有模块设置通用布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!