Today, has been a very productive day for me. Part of it was spent on picking up Zend_Navigation which, thanks to Jon Lebensold’s Zend Cast on the subject of using Zend_Navigation to create menus, sitemaps and breadcrumbs, was easier than I had expected.
Zend_Navigation makes the management of a web site’s navigation very easy as it allows you to programmatically create the navigation bar by specifying either the URI or Controller, Action and Module properties of a Zend Navigation Page.
To use Zend Navigation, all I had to do was the following:
- Add configuration entries for navigation in the application.ini file.
- Initialise and attach an instance of the Zend_Navigation container class to the navigation helper via the view in the bootstrap.
- Display the navigation bar with Zend_View_Helper_Navigation::menu() or the BreadCrumbs with Zend_View_Helper_Navigation::breadcrumbs() in your layout.phtml file.
Add configuration entries for navigation in the application.ini file
Now I could have configured the Zend_Navigation container by passing the configuration options in an array, but I thought specifying the navigation in the application.ini was easier because it would keep my init method leaner (more on this later). So the first thing I had to do was add configuration entries for navigation in the application.ini file.
Here’s a code sample:
; —
; Navigation
; —
navigation.home.label = home
navigation.home.controller = index
navigation.home.action = index
navigation.users.label = users
navigation.users.controller = system-user
navigation.users.action = index
navigation.help.label = help
navigation.help.controller = help
navigation.help.action = index
Initialise and attach an instance of the Zend_Navigation container class to the navigation helper via the view in the bootstrap
Initialise the navigation object in the bootstrap class – well there’s no explanations needed here. See the code sample.
Here’s a code sample:
protected function _initNavigation()
{
$this->bootstrap(‘layout’);
$layout = $this->getResource(‘layout’);
$view = $layout->getView();
$navigation = new Zend_Navigation($this->getOption(‘navigation’));
$view->navigation($navigation);
}
Display the navigation bar with Zend_View_Helper_Navigation::menu() or the BreadCrumbs with Zend_View_Helper_Navigation::breadcrumbs() in your layout.phtml file
The Navigation view helper has methods such as menu(), breadcrumbs() and some methods for generating a valid xml sitemap() of the navigation container. You already know this, but I’ve included a code sample of how to do this with the menu() method of the Navigation view helper for completeness.
Here’s a code sample:
<!– left –><div id=”left”><?php echo $this->navigation()->menu(); ?></div><!– /left –>

Lovely. I liked it. Plz do visit me and leave your comment on GooTAR Blog
Thanks for the comment – glad you liked it.
When your navigation is that simple(meaning no dynamic data is involved) its a bit cleaner to use the navigation bootstrap resource in my opinion:
http://framework.zend.com/manual/en/zend.application.available-resources.html#zend.application.available-resources.navigation
That way you don’t even need the _initNavigation() bootstrap method
Very interesting. I must have missed it!
Thanks for pointing it out Yv@n. I’ll be sure to check it out.