将差异布局用于差异模块zend framework 2
我正在使用EdpModuleLayouts将一种布局用于zf2 webapp的移动版本,另一种用于桌面"版本.
I am using EdpModuleLayouts to use one layout to mobile version of my zf2 webapp and another to the "desktop" version.
应用程序"模块中module.config.php中的配置:
The configuration in module.config.php in Application module:
...'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',
),
),
Application模块的Module.php就像这样:
Module.php of the Application module it's like this:
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,每次都在用户模型中渲染布局.
Finally, I have one layout in Application module and another in User module. At this moment every time render the layout in the User Model, even though I enter the Application url.
我坚持了这一点,感谢您的帮助.
I stucked on this, I appreciate some help.
我还在我的多布局项目中使用EdpModuleLayouts. 我认为,您需要将 module_layouts 从 module.config.php 移到 autoload/global.php 文件中.
I am also using EdpModuleLayouts for my multi-layout project. I think, you need to move module_layouts from module.config.php to autoload/global.php file.
这是我的应用程序"模块的 Module.php :
This is my Module.php of the Application module :
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$eventManager->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]);
}
}, 100);
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
}
这是我的 config \ autoload \ global.php :
return array(
'db' => array(
.........
),
'service_manager' => array(
...........
),
'module_layouts' => array(
'Application' => 'layout/layout.phtml',
'MyModuleName' => 'layout/mymodulename.phtml',
),
);
它对我有用,希望对您有帮助.
it works for me and hope it helps you.