Making Zend_Navigation Useful
March 31st, 2010 @ 7:00 am- An Intro To Zend_Navigation
- Making Zend_Navigation Useful
- Controlling Access: Zend_Navigation and Zend_Acl
In the last blog post, we discussed creating Zend_Navigation pages and containers. This is certainly wonderful and exciting, but the reality is that for the most part, Zend_Navigation is a pretty useless component of Zend Framework until you have a way to get the data out of the structure you’ve built. And since navigation is a component of most people’s views, we have a view helper to give us the tools we need.
When inside the view, there is a helper method called navigation() that can be accessed to do pretty much any of the things you need to do with the navigation objects. So let’s get started.
First and foremost, we need to give our navigation object the container that contains all of the pages:
$container = new Zend_Navigation(); // Some code here that puts a bunch of pages inside the container $this->view->navigation($container);
It’s that simple. We’ve now added our navigation container to the navigation view helper. There are also some other cool options we can set, from translators to ACLs (which we’ll actually cover in the next entry).
Now that the container is inside the view helper, we can work on it. We may want to find one of our objects and turn it into a hyperlink, for example. We can do this with ease, by searching for the page then using the view helper to render it:
$page = $this->navigation()->findOneBy('controller', 'home');
echo $this->navigation()->htmlify($page);
And just like that we’ve added a fully formatted hyperlink to our website. However, this isn’t the most useful thing that our navigation helper can do for us.
The navigation helper can produce menus or breadcrumbs for us. Breadcrumbs are those little helpers that tell you where you are on a site, and let you click to get back to various components. Zend_Navigation is smart; it can figure out where you are, and render that information automatically.
echo $this->navigation()->breadcrumbs();
This is an easy way to add these little details to your website. Additionally, there are a number of configuration options you can specify, including minimum depth, indentation, and whether or not to show the root item in the trail.
Generating menus is just as easy: you can output the entire menu as an unordered list (which can be styled with configuration options) by simply using the menu view helper:
echo $this->navigation()->menu();
Of course, this may not be an optimal scenario, especially if your tree is quite in depth. There is the ability to display a subset of the menu, by finding a particular set of components and rendering the menu.
$page = $this->navigation()->findOneBy('controller', 'home');
$this->navigation()->menu()->renderMenu($page);
Zend_Navigation coupled with this view helper provide a powerful set of components that make creating menus, breadcrumbs, trees and other items easy. Not covered here is the creation of a sitemap.xml document, and the creation of hyperlinks with the view helper using the link() method; however, these are easy enough to find in the view helper documentation.
We haven’t tackled ACLs yet, which is the topic of our next entry. ACLs are a subject all their own, and covering them individually is the best route to take when dealing with Zend_Navigation.
The original work of Brandon Savage.
No related posts.
Categories: Technology, Web Design, Zend FrameworkTags: , Acl, ACLs, view helper, views, Zend Framework, Zend Navigation, Zend_Navigation@The Sorrow: you can build navigatin in plugin and check if pages are active (isActive() method)
I also offen modify isActive() in MVC pages so its easier to build bigger link structures
Very good helper coverage, which is a bit lacking in the docs. Also, I think the combining with ACL is a very important topic. Looking forward to reading your next post!
Web developer, amateur photographer, traveller, and amatuer chef. Expect to find me writing code, visiting new places or trying a new recipe. I live with my wife in Olney, Maryland. Follow Me On Twitter!- Excited About PHP Again
- Rethinking The Technical Resume
- We The State, Not We The People
- Working To Defeat the Stop Online Piracy Act
- Diversifying This Blog
- What do you want the web to be?
- Why I Love Being An Engineer
- Validation Blind Spots Hurt Real Users
- Finding A Job Without A Recruiter
- Why Recruiters Are Bad For Your Career



Nice article. What about creating zend_navigation container in lazy-mode ?
For example, i have a zend_navigation container which contain my main menu and the submenu are created only when the corresponding parent main menu is accessed…
Also i would be interested in generating sitemap with zf_nav but never tried it yet…