Layout ZendFramework 2-加载不同模块的布局时出错
在Layout ZendFramework 2-加载不同模块的布局时出错,layout,module,zend-framework2,Layout,Module,Zend Framework2,在/config/application.config.php return array( 'modules' => array( 'Application', 'Admin', ) ... ... 'template_map' => array( 'layout/layout' => __DIR__ . '/../view/layout/layout.phtml', 'header' =&
/config/application.config.php
return array(
'modules' => array(
'Application',
'Admin',
)
...
...
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'header' => __DIR__ . '/../view/layout/header.phtml',
'footer' => __DIR__ . '/../view/layout/footer.phtml',
'paginator' => __DIR__ . '/../view/layout/paginator.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
)
...
...
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'header' => __DIR__ . '/../view/layout/header.phtml',
'footer' => __DIR__ . '/../view/layout/footer.phtml',
'paginator' => __DIR__ . '/../view/layout/paginator.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
)
...
我有两套独立的布局,/module/Application/view/layout/layout.phtml
和/module/Admin/view/layout/layout.phtml
在/module/Admin/config/module.config.php
return array(
'modules' => array(
'Application',
'Admin',
)
...
...
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'header' => __DIR__ . '/../view/layout/header.phtml',
'footer' => __DIR__ . '/../view/layout/footer.phtml',
'paginator' => __DIR__ . '/../view/layout/paginator.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
)
...
...
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'header' => __DIR__ . '/../view/layout/header.phtml',
'footer' => __DIR__ . '/../view/layout/footer.phtml',
'paginator' => __DIR__ . '/../view/layout/paginator.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
)
...
在/module/Application/config/module.config.php
return array(
'modules' => array(
'Application',
'Admin',
)
...
...
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'header' => __DIR__ . '/../view/layout/header.phtml',
'footer' => __DIR__ . '/../view/layout/footer.phtml',
'paginator' => __DIR__ . '/../view/layout/paginator.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
)
...
...
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'header' => __DIR__ . '/../view/layout/header.phtml',
'footer' => __DIR__ . '/../view/layout/footer.phtml',
'paginator' => __DIR__ . '/../view/layout/paginator.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
)
...
基本上,它们是不同的集合,有些内容是不同的。不幸的是,这两个模块都只加载位于/module/Admin/config/module.config.php
return array(
'modules' => array(
'Application',
'Admin',
)
...
...
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'header' => __DIR__ . '/../view/layout/header.phtml',
'footer' => __DIR__ . '/../view/layout/footer.phtml',
'paginator' => __DIR__ . '/../view/layout/paginator.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
)
...
...
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'header' => __DIR__ . '/../view/layout/header.phtml',
'footer' => __DIR__ . '/../view/layout/footer.phtml',
'paginator' => __DIR__ . '/../view/layout/paginator.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
)
...
我在谷歌上搜索了一下,但没有找到任何我想要的解决方案。有人对此有什么想法吗?您可能有兴趣知道您的配置实际上做了什么,你可能会感兴趣。最终,所有配置文件将合并为一个。每个模块的全局配置键都是而不是。) 为了达到你的目标,你应该阅读 埃文提供了使事情变得非常简单的方法。但是,如果您只需要AdminModule的一个备选布局,那么我建议您只需使用他的博客文章中的示例代码,通过
AdminModule/Module::onBootstrap
class Module
{
public function onBootstrap($e)
{
$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, '\\'));
if ('AdminModule' === $moduleNamespace ) {
$controller->layout('layout/admin');
}
}, 100);
}
}
这不会将布局设置为layout/admin
。您需要通过配置提供此密钥:
'template_map' => array(
'layout/admin' => 'path/to/admin/module/view/layout/admin.phtml',
)
在ZF2.2.2中,我必须替换$moduleNamespace变量,然后它对我有效:
if('Admin'====$moduleNamespace){$controller->layout('layout/Admin');}