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.
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:
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.
Frustrated with your company’s development practices?
You don't have to be!
No matter what the issues are, they can be fixed. You can begin to shed light on these issues with my handy checklist.
Plus, I'll help you with strategies to approach the issues at the organization level and "punch above your weight."