2011-05-05 30 views
1

我想添加一個下拉菜單到我的佈局。 我有一個酒店列表。我想從下拉菜單中更改酒店,我需要在layout.phtml中保留該下拉菜單。 問題是酒店名單是動態的。 我可以做到這一點,這可能在Zend中,我如何在zend框架中添加一個dynamiclally生成下拉列表layout.phtml

這裏是我的佈局,PHTML

我需要一個下拉列表添加到<div class="floatright wid35 textaligncenter padtop5">

<html> 
<head> 
    <?php echo $this->docType(); ?> 
    <?php echo $this->headTitle(); ?> 
    <?php echo $this->headScript(); ?> 
    <?php echo $this->headLink(); ?> 
    <?php echo $this->headStyle(); ?> 
</head> 
<body> 

<?php echo $this->render('admin/header.phtml'); ?> 


<div id="contentColumns" class="columns"> 
    <div id="columnRight" class="column-right"></div> 
    <div id="columnLeft" class="column-right"> 
     <div class="link-guide"> 
      <div id="breadcrumbs" class="floatleft wid60"> 
       <?php echo $this->navigation()->breadcrumbs()->setLinkLast(false)->setMinDepth(0)->render(); ?> 
      </div> 
      <div class="floatright wid35 textaligncenter padtop5"> 

      </div> 
     </div> 
     <div class="padding-box"> 
      <?php echo $this->layout()->content; ?> 
     </div> 
    </div> 
    <div class="clear"></div>  
</div>     

<?php echo $this->render('admin/footer.phtml'); ?> 
</body> 
</html> 
+0

實際上,該案是形式應該出現在我的所有頁面, – 2011-05-05 11:20:04

回答

3

號佈局文件代表的靜態結構一個像頁眉和頁腳的頁面,以及包圍動態內容的所有內容。

所以這不被推薦。然而,你能解決,實施你的主控制器的_init方法,並與主控制器擴展任何控制器:

class MainController extends Zend_Action_Controller{ 

    function init(){ 
     $this->view->foo = "Show everywhere!"; 
    } 
} 

class IndexController extends MainController{ 

    public function indexAction(){ 
     $this->view->bar = "Show only on index/index"; 
    } 
} 

或者你可以使用一個插件,這將是更優雅的方式

class MyPlugin extends Zend_Controller_Plugin_Abstract{ 

    public function preDispatch(Zend_Controller_Request_Abstract $request){ 

     $view = Zend_Controller_Action_HelperBroker:: 
         getStaticHelper('viewRenderer')->view; 

     $view->foo = "bar"; 
    } 
} 

和在你的引導過程中註冊該插件

Zend_Controller_Front::getInstance()->registerPlugin(new MyPlugin); 
+0

爲什麼張貼我的想法的副本???這只是玩笑,和平;) – Iscander 2011-05-05 13:25:23

+0

:D:D:D容易...;) – 2011-05-05 13:31:59

+0

毫米.......試圖理解這一點。我將能夠做到這一點。感謝您的幫助球員:D :) – 2011-05-05 13:34:40

1

你可以在你的基礎控制器類中創建preDispatch函數。 然後你得到酒店列表並將其發送給視圖:

$this->view->hotels = $hotels; 

而在你的佈局,如你所願,你可以解析它。

+0

也可以把邏輯創建下拉部分​​(爲乾淨的佈局) – Iscander 2011-05-05 13:02:01

+0

爲什麼張貼dublicate我的答案? – 2011-05-05 13:11:09

+0

我認爲引導是這個功能的錯誤的地方,但我可能是錯的;) – Iscander 2011-05-05 13:13:19

0

在這裏我看到兩個問題:

  1. 在下拉使用你的酒店名單表明,你將有My_Form_Hotels延伸Zend_Form一種形式,接受酒店列表作爲構造函數的參數。

  2. 你應該在哪裏填充酒店列表,實例化窗體,並將所有這些信息傳遞給視圖/佈局進行渲染?

聽起來像front controller plugin會做這項工作。由於佈局資源作爲插件在postDispatch()中延遲運行,因此您需要在此之前運行您的插件。 dispatchLoopStartup()應該沒問題。

像這樣的東西在library/My/Plugin/HotelForm.php

class My_Plugin_HotelForm extends Zend_Controller_Plugin_Abstract 
{ 
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) 
    { 
     $hotelModel = new My_Model_Hotel(); 
     $hotels = $hotelModel->getAllHotels(); 
     $form = new My_Form_Hotels($hotels); 
     $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer'); 
     $view = $viewRenderer->getView(); 
     $view->hotelForm = $form; 
    } 
} 

然後在你的佈局,只需用傾倒的$this->hotelForm形式。

爲了使您的插件,添加到application/configs/application.ini

autoloaderNamespaces[] = 'My_" 
resources.frontcontroller.plugins[] = "My_Plugin_HotelForm" 
相關問題