我使用EdpModuleLayouts来使用一个布局到我的zf2 webapp的移动版本和另一个到“桌面”版本。使用不同的布局到不同的模块zend框架2
在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_map' => array(
'module_layouts' => array(
'Application' => 'layout/application',
'User' => 'layout/user',
),
'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',
),
),
Module.php的应用模块,它就像这样的:
public function onBootstrap(MvcEvent $e)
{
$e->getApplication()->getServiceManager()->get('translator');
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$e->getApplication()->getEventManager()->getSharedManager()
->attach('Zend\Mvc\Controller\AbstractActionController', '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]);
echo $config['module_layouts'][$moduleNamespace];
}
}, 100);
}
最后,我在应用模块一个布局另一个在用户模块中。此时每次在用户模型中渲染布局,即使我输入了应用程序url。
我对此感到困惑,我感谢一些帮助。
你为什么复制代码?您是使用他的模块还是仅使用他的模块代码?此外,在你的配置中有一个错误,'module_layouts'应该是一个顶层条目。请检查一些关于此主题的其他主题,其中有10-20个;) – Sam 2013-05-03 08:29:57