Zend_Navigation update

Continuing from the breadcrumbs post, it is possible to render more than one zend navigate menu. The bootstrap code looks for different sections of the xml, which we tweak like this:


and the bootstrap we can say:

$config = new Zend_Config_Xml(APPLICATION_PATH.'/configs/navigation.xml','nav');
 $view->headnav = new Zend_Navigation($config->headnav);
 $view->footnav = new Zend_Navigation($config->footnav);
 Zend_Registry::set('HeadNavigation', $view->headnav);
 Zend_Registry::set('FootNavigation', $view->footnav);

we can now use both menus in our layout like this:

echo $this->navigation()->menu()->renderMenu($this->headnav);
 echo $this->navigation()->menu()->renderMenu($this->footnav);

However I am yet to discover what to do to get the breadcrumbs working like this!

Navigation, Breadcrumbs, & Sitemap with Zend Framework

Let Zend Framework do all the work when it comes to your Navigation, and stop hard coding stuff in either layouts or view files!

First up, in the configs folder, create an XML file called navigation.xml :

<?xml version="1.0" encoding="UTF-8"?>
       <label>About Us</label>
       <label>Contact Us</label>
       <label>Site Map</label>
       <label>Privacy Policy</label>
       <label>Terms of Use</label>

Stick this _init method into your bootstrap :

protected function _initNavigation()
 $layout = $this->getResource('layout');
 $view = $layout->getView();
 $config = new Zend_Config_Xml(APPLICATION_PATH.'/configs/navigation.xml','nav');
 $nav = new Zend_Navigation($config);

This loads the XML into a Zend Config, which gets passed to Zend_Navigate, and finally to the view. To get this stuff displaying on your pages, go into your layout.phtml and add this:

echo $this->navigation()->menu();

For breadcrumbs, put this into your Controllers _init method. This figures out where in the XML tree we are:

$uri = $this->getRequest()->getPathInfo();
if (($activeNav = $this->view->navigation()->findByHref($uri)) !== null) 
 $activeNav->active = true;
else if (($activeNav = $this->view->navigation()->findByUri($uri)) !== null) 
 $activeNav->active = true;

And back in the layout, add this:

echo $this->navigation()->breadcrumbs()->setMinDepth(0)->setLinkLast(false)->setSeparator(' | ');

The setLinkLast false makes the last breadcrumb non linkable (I mean we’re on that page!), and setSeparator can contain any HTML.

Finally, to get a sitemap that Google can accept, in your sitemap’s controller action, we only need to add this :

 echo $this->view->navigation()->sitemap();

And thats us! Nice n easy! Bonus brownie points for anyone who posts a reply showing how to separate footer links from the nav!