ZF2 已经为所有模块提供了通用共享布局。默认为layout/layout.phtml
. 只需将此文件放在应用程序模块视图目录中,它将为所有模块共享。
所有加载模块的所有视图脚本都可以在其他模块中使用,您只需要为视图模型提供模板路径:
$viewModel->setTemplate('application/index/index');
这些是默认值:
$viewModel->setTemplate('[module namespace]/[controller]/[action]');
但是当您手动提供模板路径时,它们可以是注册视图目录中的任何内容。
您还可以在视图管理器配置中更改默认布局文件路径:
'view_manager' => array(
'display_not_found_reason' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
// 'layout' => 'layout/layout.phtml',//<----------------------------
'template_path_stack' => array(
__DIR__ . '/../view',
),
'strategies' => array(
'ViewJsonStrategy',
),
),
或在控制器本身:
$this->layout('layout/layout.phtml');
或者在某个事件中,如果您需要它是动态的。
为您查看需要更改/或添加到视图管理器配置中的文件定义另一个目录
'template_path_stack' => array(
__DIR__ . '/../view',
),
或者如果你需要它是动态的:
$templatePathResolver = $this->getServiceLocator()->get('Zend\View\Resolver\TemplatePathStack');
$templatePathResolver->setOptions(
array(
'script_paths' => array(
$client_theme_path,
)
)
);