Zend_Navigation makes writing Navigation for ZF Sites Very Easy

January 15th, 2010 by rvdavid Leave a reply »

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 –>
So there you have it. A nicely organised and easily configurable navigation system for your Zend Framework MVC Web Application. I hope you find this helpful. It sure was to me.
References:

You may also be interested in the following posts

Advertisement

4 comments

  1. GooTAR says:

    Lovely. I liked it. Plz do visit me and leave your comment on GooTAR Blog

  2. Yv@n says:

    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 :)

Leave a Reply