2013-12-16 133 views
1

我想要動態模塊配置。想象一下你有一個表格,該表格保存了要使用哪種設計的信息。我需要能夠從這個表中注入一個結果,所以我可以動態地指定視圖文件的路徑。Zend Framework 2,動態模塊配置

結構:

的index.php
模塊
--application
----配置
------ module.config.php
--Album
- 更多....
設計
--blue
----佈局
----意見
------鴨摺疊
------專輯
--default

我需要能夠處理最新的設計數據庫的使用,然後使用在module.config.php所以模板路徑可以使用設計文件夾。

事情是這樣的:

'template_map' => array(
    'layout/layout'   => __DIR__ . '/../../../design/'.THEME.'/layouts/layout.phtml', 
    'application/index/index' => __DIR__ . '/../../../design/'.THEME.'/views/application/index/index.phtml', 
), 
'template_path_stack' => array(
    __DIR__ . '/../../../design/'.THEME.'/views', 
), 

但是,如果我下面用內部模塊的引導

$eventManager->attach('bootstrap', array($this, 'loadThemingConfig'), 100); 

將加載配置文件後得到執行。

我該如何實現我所需要的?

回答

3

我建議留下默認主題的路徑,像這樣的配置:

'template_map' => array(
    'layout/layout'   => __DIR__ . '/../../../design/default/layouts/layout.phtml', 
    'application/index/index' => __DIR__ . '/../../../design/default/views/application/index/index.phtml', 
), 
'template_path_stack' => array(
    __DIR__ . '/../../../design/default/views', 
), 

,並在你的模塊類改變它在運行時,你可以做,在MVC渲染事件,例如:

use Zend\Mvc\MvcEvent; 
use Zend\View\Resolver\TemplateMapResolver; 
use Zend\View\Resolver\TemplatePathStack; 

public function onBootstrap(MvcEvent $e) 
{ 
    $eventManager->attach(MvcEvent::EVENT_RENDER, function(MvcEvent $event) { 
     $sm = $event->getParam('application')->getServiceManager(); 

     /** @var TemplateMapResolver $viewResolverMap */ 
     $viewResolverMap = $sm->get('ViewTemplateMapResolver'); 
     // modify template map 

     /** @var TemplatePathStack $viewResolverPathStack */ 
     $viewResolverPathStack = $sm->get('ViewTemplatePathStack'); 
     // modify path stack 
    }, 10); 
}