Zend Framework navigation with breadcrumbs


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:

1
2
3
4
5
6
7
8
9
10
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:

1
2
3
4
5
6
7
8
9
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:

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

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

1
2
3
4
< ?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.

Source: inchoo.net

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s