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"?>
<configdata>
 <nav>
  <home>
   <label>Home</label>
   <uri>/</uri>
   <pages>
     <buying>
       <label>Buying</label>
       <uri>/buying</uri>
     </buying>
     <selling>
       <label>Selling</label>
       <uri>/selling</uri>
     </selling>
     <tenants>
       <label>Tenants</label>
       <uri>/tenants</uri>
     </tenants>
     <landlord>
       <label>Landlord</label>
       <uri>/landlord</uri>
     </landlord>
     <about>
       <label>About Us</label>
       <uri>/about-us</uri>
     </about>
     <contact>
       <label>Contact Us</label>
       <uri>/contact-us</uri>
     </contact>
     <sitemap>
       <label>Site Map</label>
       <uri>/sitemap</uri>
     </sitemap>
     <privacypolicy>
       <label>Privacy Policy</label>
       <uri>/privacypolicy</uri>
     </privacypolicy>
     <termsofuse>
       <label>Terms of Use</label>
       <uri>/termsofuse</uri>
     </termsofuse>
   </pages>
  </home>
 </nav>
</configdata>

Stick this _init method into your bootstrap :

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

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 :

Zend_Layout::getMvcInstance()->disableLayout(); 
 $this->_helper->viewRenderer->setNoRender(true);
 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!

Advertisements