| Latest |Kites |Pictures |Programming |Life |
![]() [filed under Programming]Zend Framework Menus Navigation ![]() Tags: Zend Framework, menus, controller, action, navigation, template, menu.phtml, This is how to put menu logic in your web page. OK, from the get go, this is hard. You may need to read it a couple of times or do more research. What we want is a web page with a menu system (a list of items) on the left of the page. The menu will be different for each controller and action. What usually happens in ZF is the ApplicationController class is created and the method indexAction is called. indexAction then uses the model to get data and passes info to the view (for example using $this->view->data = 99; ). The view is then rendered as a string, which is appended to the Layout content. It took me a while to figure that out. To make the menu we need a seperate controller, the MenuController will decide what to show on the menu. The the menu view will render it and we capture the output of the view into a layout variable. The menu HTML is used later in the layout.phtml template. In our application controller we push the "menu" onto the actionStack which makes ZF run MenuController::applicationAction next.
MenuController.php The MenuController changes the layout response segment to 'menu', all the HTML in the menu view is saved to this variable. This HTML to make the menu is then merged into the web page in layout.phtml. More notes on Zend Framework Menu Navigation. Comments [20th of Sep, 2008 @ 01:35 PM] Pete Williams said: Thank you! I've been trying to get my head around this for about 2 days now and now it finally makes sense! [29th of Sep, 2008 @ 10:32 AM] ignar said: Thanks. You help me much. [13th of Nov, 2008 @ 11:07 AM] David said: Firstly, what is your view on this approach? Adding an actionStack call to every controller seems long winded especially if the majority use a standard menu i.e. defaultmenuAction in the NavController. Also using this method I am encountering a problem with the use of _forward() in my actions. When calling a _forward from one action to another within the same controller the menu action is added to the actionStack twice (once for initial init() and then for each forward). How would you propose I stop this easily? Is there a way of detecting if the actionStack currently has the call to the action in the NavController and thus doesnt add it again? Back to my original question... is there a better way entirely? Thanks for your help. [20th of Nov, 2008 @ 11:39 AM] Pieter said: David, You can create an Zend_Controller_Action class that all your controllers can inherit and only do the actionStack in the Parent class like this: public function __construct($req, $rsp) { parent::__construct($req, $rsp); $this->_helper->actionStack('menu','nav'); } Haven't tested this but I'm sure it should work |
| Server Grind [0.0036 seconds] |