2011-10-06 137 views
9

我使用各種模塊和父控制器來保持一致性。Zend Framework在模塊/控制器中渲染不同的視圖

我的父控制器有一系列的後續子控制器需要訪問的一些常見操作。我發現很難讓這個默認行爲呈現一個正確的視圖。

我不想爲每個模塊添加相同的視圖 - 這將打敗對象 - 但我想有一個默認視圖。

我曾嘗試:

$this->_forward('commonaction', 'baselayout', 'default'); 

這對我不起作用 - 因爲它試圖再次處理的動作 - 當我已經填寫我的父控制器內所需的變量。

任何幫助將是awesom。

UPDATE:

爲了澄清我希望能夠使用共同的視圖從不同的模塊。所有示例和解決方案目前都採用通用模塊。這不適合我。

+0

我已經更新了我的答案。 Aurelio在正確的軌道上,但是設置添加腳本路徑是在當前模塊之外使用視圖腳本所必需的。 –

回答

5

使用此聲明在你的控制器:

// Render 'foo' instead of current action script 
$this->_helper->viewRenderer('foo'); 

// render form.phtml to the 'html' response segment, without using a 
// controller view script subdirectory: 
$this->_helper->viewRenderer('form', 'html', true); 
從官方文檔 http://framework.zend.com/manual/en/zend.controller.actionhelpers.html

這是不可能的(在我所知的)來顯示不同的觀點模塊。除官方文檔中沒有這個問題的痕跡外。

+0

嘿Aurelio - 感謝這個解決方案的主要問題是,它仍然在我目前所在的同一個模塊中尋找視圖。這是錯誤的。我需要能夠說我想從模塊Y渲染的視圖X. – Andy

+0

對不起。我瞭解同一模塊,但不同的控制器。 –

+0

啊好吧我的壞。將相應地更新我的問題。你知道這有可能嗎? – Andy

7

我固定它使用:

$this->_helper->viewRenderer->renderBySpec('foo', array('module' => 'modulename', 'controller' => 'controllername')); 
+1

它不接受'module'的值,Zend 1.12 – Herokiller

-1

你可以嘗試(Zend Doc):

$View = new Zend_View(); 
$View->addScriptPath('/path/to/script'); 
+0

這實際上並不是OP所要求的。設置腳本路徑不會呈現不同的視圖。 –

2

你一樣,我有多個模塊,並希望使任意視圖(駐留在一個模塊中)在不同模塊的控制器中。

這是我能得到它的工作,嘗試很多方法後的唯一途徑:

控制器動作:

public function xyzAction() 
{ 
    $this->_helper->layout->disableLayout(); 
    $view = new Zend_View(); 
    $view->setScriptPath(APPLICATION_PATH . '/modules/moduleB/views/scripts/abcBlahBlah'); 
    echo $view->render('yadaYada.phtml'); 
    exit; 
} 
2

嘗試在你的行動如下:

  • 第一您指定存儲視圖腳本的模塊的路徑
  • 然後,您將視圖腳本的名稱指定給視圖渲染器

請看下面的代碼:

// Add a script path to the directory in the other module 
$this->view->addScriptPath(APPLICATION_PATH . '/modules/baselayout/views/scripts'); 
// Tell the viewRenderer to use the view script 'scriptname' 
$this->_helper->viewRenderer('scriptname'); 
相關問題