i want to add a dropdown menu to my layout . i have a list of hotels . i want to change the hotel from a dropdown , i need to keep that drop down in my layout.phtml . problem is hotel list is dynamic. can i do this , is this possible in zend ,
here is my layout,phtml
i need to add a dropdown to <div class="floatright wid35 textaligncenter padtop5">
<html>
<head>
<?php echo $this->docType(); ?>
<?php echo $this->开发者_JAVA百科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>
No. Layout files represent the static structure of a page like header and footer and everything you wrap around your dynamic content.
So this is not recommended. However you could solve that implementing the _init
method of your main controller and extend any controller with that main controller:
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";
}
}
Or you can use a plugin which would be the more elegant way
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";
}
}
and in your bootstrap process register that plugin
Zend_Controller_Front::getInstance()->registerPlugin(new MyPlugin);
You can create preDispatch function in your base controller class. Then you get hotel list and send it to view:
$this->view->hotels = $hotels;
And in your layout you can parse it as you wish.
I see two issues here:
Using your hotel list in a dropdown suggests that you will have a form
My_Form_Hotels
extendingZend_Form
which accepts the hotel list as a constructor parameter.Where should you populate the hotel list, instantiate the form, and communicate all this to the view/layout for rendering?
Sounds like a front controller plugin would do the job. Since the layout resource functions as a plugin that runs late in postDispatch()
, you need to run yours before that. dispatchLoopStartup()
should be fine.
Something like this in 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;
}
}
Then in your layout, simply dump the form using $this->hotelForm
.
To enable your plugin, add to application/configs/application.ini
:
autoloaderNamespaces[] = 'My_"
resources.frontcontroller.plugins[] = "My_Plugin_HotelForm"
精彩评论