Blog chia sẻ và thảo luận về IT|Programming Language|Search Engine Optimization|Data Base|Linux| ...

datnguyen

Zend Framework, setting up a dynamic menu using a plugin

+ No comment yet
I wanted to set up the menu in layout.phtml using a dynamic call to the database.
I created a directory in library called plugins.
I put this in application.ini
autoloaderNamespaces[] = “Plugins”
I created a class in the plugins directory called Plugins_Layout with the file name Layout.php
  1. class Plugins_Layout extends Zend_Controller_Plugin_Abstract  
  2. {  
  3.     public function preDispatch(Zend_Controller_Request_Abstract $request)  
  4.     {  
  5.         $layout = Zend_Layout::getMvcInstance();  
  6.         $view = $layout->getView();  
  7.         $content_mapper = new Application_Model_ContentMapper();  
  8.         $view->menu = $content_mapper->getMenu();  
  9.     }  
  10. }  
I then added this to Bootstrap.php
  1. protected function _initRoutes() {  
  2.         ......   
  3.        Zend_Controller_Front::getInstance()->registerPlugin(new Plugins_Layout());  
  4.     }  
I was then able to create the dynamic menu in layout.phtml
  1. <!--?php foreach($this--->menu as $url => $title) { ?>  
  2.   
  3. a href="/<?php echo $url ?>"><!--?php echo $title; ?--></a>  
  4.   
  5. <!--? } ?-->  
All was working as I expected.
Nguồn : http://blog.saunderswebsolutions.com
Hiển thị các bài đăng có nhãn Create dynamic menu. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn Create dynamic menu. Hiển thị tất cả bài đăng