我如何在zend框架中添加一个dynamiclally生成下拉列表给layout.phtml

时间:2011-05-05 10:12:31

标签: zend-framework

我想在我的布局中添加一个下拉菜单。 我有一个酒店列表。我想从下拉列表中更改酒店,我需要在layout.phtml中保留该下拉菜单。 问题是酒店列表是动态的。 我能做到这一点,这是否可能在zend,

这是我的布局,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>

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分:1)

您可以在基本控制器类中创建preDispatch函数。 然后你得到酒店列表并发送到视图:

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

在您的布局中,您可以根据需要进行解析。

答案 2 :(得分:0)

我在这里看到两个问题:

  1. 在下拉列表中使用您的酒店列表表示您将使用My_Form_Hotels扩展Zend_Form表单,该表单接受酒店列表作为构造函数参数。

  2. 您应该在哪里填充酒店列表,实例化表单,并将所有这些传达给视图/布局进行渲染?

  3. 听起来像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"