Zend Framework-导航设置当前页面标题并打开标记标题导航.xml

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Zend Framework-导航设置当前页面标题并打开标记标题导航.xml相关的知识,希望对你有一定的参考价值。

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.
  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"

以上是关于Zend Framework-导航设置当前页面标题并打开标记标题导航.xml的主要内容,如果未能解决你的问题,请参考以下文章

通用页面的 Zend Framework 路由?

PHP Zend Framework:设置Zend Navigation

Zend Framework 2 设置全局模块变量

zend framework 重定向方法(render, forward, redirect)总结

PHP Zend Framework:将表单重定向回页面用户来自

Zend Framework 2 入门