我使用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