2013-05-03 91 views
5

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

我對此感到困惑,我感謝一些幫助。

+3

你爲什麼複製代碼?您是使用他的模塊還是僅使用他的模塊代碼?此外,在你的配置中有一個錯誤,'module_layouts'應該是一個頂層條目。請檢查一些關於此主題的其他主題,其中有10-20個;) – Sam 2013-05-03 08:29:57

回答

16

我也在爲我的多佈局項目使用EdpModuleLayouts。 我想,你需要移動module_layoutsmodule.config.phpautoload/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); 
} 

這是我配置\自動加載\ global.php

return array(
    'db' => array(
     ......... 
    ), 
    'service_manager' => array(
     ........... 
    ), 
    'module_layouts' => array(
     'Application' => 'layout/layout.phtml', 
     'MyModuleName' => 'layout/mymodulename.phtml', 
    ), 
); 

它爲我工作和希望它可以幫助你。

2

我的解決辦法:

  1. 寫控制器插件;
  2. 在插件:

    $modNameArray = explode('\\', $event->getRouteMatch()->getParam('controller')); 
    $modName = $modNameArray[0]; 
    $viewModel->setTemplate(strtolower($modName).'/layout'); 
    

獲得模塊名稱,至少是你的控制器的第一個目錄名,在我的應用程序。

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

爲我工作。

+0

+1,用於爲佈局提供每個模塊的唯一標識符。 – kta 2014-03-29 13:08:29

2

以下適用於我。

主要想法是爲佈局命名一個不同的標識符,而不是使用像'佈局/佈局'這樣的常用名稱。通過這種方式,當配置開始合併時,它不會迷路。

如果我有一個模塊名稱專輯,然後我會有以下。

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

和應working.Hope這有助於:)

+1

我在使用多個模塊時發現,命名空間中包含部分和錯誤的所有模板是一種很好的做法。 – 2014-04-23 00:08:43

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的文件並將您的佈局代碼那裏。

+0

這是最好的和簡單的解決方案 – tradebel123 2015-01-03 22:30:47