Zend_Navigation with hidden pages

谁说我不能喝 提交于 2019-12-24 10:28:24

问题


I have my Zend_Navigation loaded from a PHP array (but that's irrelevant...) and I'm using the navigation menu helper to generate a menu based on the loaded navigation. Some menu items must not appear in the outputted menu, so I simply set "'visible' => false" in my array for that page and there you go! But if the URL of an 'hidden' menu is accessed, the findActive($container) view helper method returns an empty array, thus the page from the container is not returned, even if it should (like if the page didn't exist); leaving the browser title empty, etc.

Since both the menu navigation helper and the navigation view helper uses the 'visible' option to discard the page (through the method accept($page)), this setting is useless in my case.

What would be the best way to go from here?


回答1:


I actually just found a much more elegant solution. Simply add the following line before your findActive() call, and it will return an invisible page, if selected:

$this->navigation()->setRenderInvisible(true);

For example, the following code:

Zend_Debug::dump($this->navigation()
                      ->findActive($this->navigation()->getContainer()));
$this->navigation()->setRenderInvisible(true);
Zend_Debug::dump($this->navigation()
                      ->findActive($this->navigation()->getContainer()));

Produces:

array(0) {
}
array(2) {
  ["page"] => object(Zend_Navigation_Page_Mvc)#33 (24) {
    ... PAGE INFORMATION ...
  }
  ["depth"] => int(0)
}

The curious part is that it doesn't affect the rendering of the menu - i.e. hidden pages are still hidden. That doesn't make much sense though, so I'd recommend setting it to false again to make sure it doesn't cause problems in the future.




回答2:


well, after some tinkering, I finally chose this option:

  1. I have an extra option for the page that I don't want to show in my menu: "menuItem". (this option is not mandatory and may be null/unset)
  2. in my layout's script, I iterate recursively through all the pages and set $page->visible = false; on all pages that false === $page->menuItem is true
  3. I call the menu navigation helper

Since when the menu navigation helper is called only after the view script has been called (headTitle is set), and the check is done in my layout, then I can safely set any page's visible property to false without negative drawbacks.



来源:https://stackoverflow.com/questions/3162902/zend-navigation-with-hidden-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!