对不同的模块zend框架2使用不同的布局

时间:2013-05-03 03:19:46

标签: layout zend-framework2

我正在使用EdpModuleLayouts将一个布局用于我的zf2 webapp的移动版本,另一个布局用于“桌面”版本。

Application模块中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',
    ),
),

Application模块的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);

}

最后,我在Application模块中有一个布局,在User模块中有另一个布局。此时,即使我输入了应用程序URL,每次都会在用户模型中呈现布局。

我坚持这一点,我感谢你的帮助。

4 个答案:

答案 0 :(得分:19)

更新你的module.config.php

'view_manager' => array(
    'template_path_stack' => array(
        'admin' => __DIR__ . '/../view',
    ),
    'template_map' => array(
        'admin/layout' => __DIR__ . '/../view/layout/layout.phtml',
    ),
),

现在在module.php中写下以下行

use Zend\ModuleManager\ModuleManager;

public function init(ModuleManager $mm)
    {
        $mm->getEventManager()->getSharedManager()->attach(__NAMESPACE__,
        'dispatch', function($e) {
            $e->getTarget()->layout('admin/layout');
        });
    }

现在在模块的视图目录中创建一个文件夹布局,并创建一个名为layout.phtml的文件,并将布局代码放在那里。

答案 1 :(得分:16)

我也在我的多布局项目中使用EdpModuleLayouts。 我想,您需要将 module_layouts module.config.php 移至 autoload / global.php 文件。

这是我的应用程序模块的 Module.php

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',
   ),
);

它对我有用,希望对你有所帮助。

答案 2 :(得分:2)

我的解决方案:

  1. 编写控制器插件;
  2. 在插件中:

    $modNameArray = explode('\\', $event->getRouteMatch()->getParam('controller'));
    $modName = $modNameArray[0];
    $viewModel->setTemplate(strtolower($modName).'/layout');
    
  3. 获取模块名称,这是控制器的第一个目录名,至少在我的应用程序中。

    1. 调整你的module.config.php

       'template_map' => array(
           //moduleName/layout => your layout path
          'auth/layout' => __DIR__ . '/../view/layout/auth.phtml',
          'auth/index/index' => __DIR__ . '/../view/auth/index/index.phtml',
          'error/404' => __DIR__ . '/../view/error/404.phtml',
          'error/index' => __DIR__ . '/../view/error/index.phtml',
      ),
      
    2. 适合我。

答案 3 :(得分:2)

以下适用于我。

主要思想是将布局命名为不同的标识符,而不是使用像'layout / layout'这样的通用名称。这样,当配置开始合并时,它就不会在途中丢失。

如果我有一个模块名称Album,那么我将有以下内容。

public function onBootstrap($e)
{
    $e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\Mvc\Controller\AbstractController', 'dispatch', function($e) {
    $controller = $e->getTarget();
    $controllerClass = get_class($controller);
    $moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
    $controller->layout($moduleNamespace . '/layout');
    }, 100);
}  

请注意,与EdpModuleLayouts相比,这有点不同。和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(
        'Album/layout'           => __DIR__ . '/../view/layout/layout.phtml',
        'Album/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',
    ),
),

这应该有效。希望这有助于:)

相关问题