Zend Framework - Navigation set current page title with tag title on navigation.xml


/ Published in: PHP
Save to your folder(s)

If you have Zend_Navigation in your application, you will use navigation.xml. This code set the page title ($this->view->headTitle()) with the current page title in navigation.xml.
$navigation->current() doesn't work in my opinion.


Copy this code and paste it in your HTML
  1. /configs/navigation.xml
  2.  
  3. <nav>
  4. <home>
  5. <title>Home</title>
  6. <label>Home</label>
  7. <module>admin</module>
  8. <controller>index</controller>
  9. <action>index</action>
  10. </home>
  11. <communities>
  12. <title>Communities</title>
  13. <label>Communities</label>
  14. <module>admin</module>
  15. <controller>communities</controller>
  16. <action>index</action>
  17. </communities>
  18. </nav>
  19.  
  20.  
  21.  
  22. /library/App/Controller/Plugin/Navigation.php
  23. class App_Controller_Plugin_Navigation extends Zend_Controller_Plugin_Abstract
  24. {
  25. public function preDispatch(Zend_Controller_Request_Abstract $request)
  26. {
  27. //$view = Zend_Registry::get('view');
  28. $view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view');
  29. $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'nav');
  30. $navigation = new Zend_Navigation($config);
  31. $view->navigation($navigation);
  32. //search current page's title on navigation.xml by Controller Name
  33. $pages = $view->navigation()->findAllByController($request->getParam('controller'));
  34. if (count($pages) == 1) {
  35. $navPage = current($pages);
  36. } else {
  37. $navPage = $this->_getCurrentPageFromController($pages, $request->getParam('action'));
  38. }
  39. $translate = Zend_Registry::get('Zend_Translate');
  40. $view->headTitle()->prepend($translate->_($navPage->getTitle()));
  41. }
  42.  
  43. protected function _getCurrentPageFromController(array $pages, $action) {
  44. foreach ($pages as $page) {
  45. if ($page->action == $action) {
  46. return $page;
  47. }
  48. }
  49.  
  50. throw new Exception("Can't find page in navigation...");
  51. }
  52. }
  53.  
  54. /configs/application.ini
  55. resources.frontController.plugins.Navigation = "App_Controller_Plugin_Navigation"

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.