Zend Framework navigation with breadcrumbs

Featured Image

It’s been a while since I last time played with beauty of Zend Framework.

Each time I check new features it seems to me that the things are more simplier than the last time,
I might be wrong (I wouldn’t bet on that) or I’m just getting better 🙂

Let’s check this piece of cake – how you can create navigation with breadcrumbs in Zend Framework project.
As many things in Zend Framework you can get same features in different ways,
so the same thing goes with the Zend_Navigation class.

First of all you need to decide which way you will choose:
– navigation with xml setup file or
– from application.ini file

I suggest you to try both ways and then choose the one you like most, I’ll show you the both ways.

In Boostrap.php class you need to initialize your navigation.
– xml way tells where your navigation.xml file is:

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

– or in “config” way from application.ini file you simply tell your Bootstrap class which part needs to be processed:

protected function _initNavigationConfig()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
 
$navigation = new Zend_Navigation($this->getOption('navigation'));
$view->navigation($navigation);
}

Now you have setup your configuration for navigation, next thing you need to do is in your
application/layouts/layout.phtml file call the data you insert into your application.ini or navigation.ini files, you do that with this line of code:

< ?php
echo $this->navigation()->menu();
?>

For desert you can add breadcrumbs into same file like this:

< ?php
echo $this->navigation()->breadcrumbs()
->setLinkLast(false)->setMinDepth(0)->render();
?>

At the end you get something like on picture below:

Here is full featured project zf.navigation.sample (without Zend Framework, which you need to copy in library folder) to download.

Enjoy coding.


4 comments

  1. i am using to generate dynamic url paths like i have city controller and city action. In that city action i have dropdown with city names. When i change cityname in city dropdown , i generated url with cityname(Ex:public/india-properties or us-properties), but controller and actions are same. So want dynamic city name based navigation can you try once.

  2. Harish

    You can replace the method htmlify
    with class extends
    Zend_View_Helper_Navigation_Menu and setting your helper path.

  3. I am using array instead of XML.
    Any Way
    I want to add custom classes to main UL and submenu UL. How can i do that ???

  4. This post was super helpful. I didn’t know making navigation elements were so easy in Zend Framework. I think the hardest part of this may be to make the XML file. I’ll share this and take a look at your other posts.

    Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.