Zend_Navigation – creating a menu, a sitemap and breadcrumbs
June 3rd, 2009This tutorial covers a lot of the basic use cases with Zend_Navigation. Using a uri-based navigation.xml file, we generate a very simple menu system as well as a breadcrumb with some basic information. In the last 3 minutes I cover how to get a Google compliant sitemap from the navigation.xml file. This video is by no means exhaustive, but should get you up and running with Zend_Navigation in the Zend Framework.
Browse the code off google code or download a zip here.
On another note, I’m thinking of putting together a forum as a way of organizing some of the informative discussion I’ve found in the comments section. I know other forums exist so I’d be curious to know if you think zendcasts would benefit from such a thing.
Alternatively, if you have topics you’d like covered and are willing to step up to the mic, I’d love to share your own screencasts with the now over 550 subscribers!

Hi Jon,
This is a very helpful tutorial! How do you assign an ACL to this menu so that the menu changes accordingly to the permissions associated with the user? My current solution is a fixed menu once user logs in which is not that efficient. Here’s the scenario:
User goes to website as:
1. Guest Account:
Home
– About
2. Member Account:
Home
– Member
+ Profile
– About
3. Admin Account:
– Admin
+ Manage Users
– About
Something like that? Thanks!
Hey, i’m really affraid about this Videotutorials …
It’s an awesome ressource on the Web … Keep up the good work …
Greetz from germany …
very helpful tuts! keep on shinning! thanks!
Hi all,
Here’s the resource added to nav xml:
User
/user
free_account_section
Got the thing working by placing the navigation object in acl:
$this->_view->navigation()->setAcl($acl)
->setRole($getRank());
where $user is a class having this:
public function getRank() {
if ($auth->hasIdentity()) return Roles::FREE;
return Roles::GUEST;
}
Regards,
Burn
That’s <resource>free_account_section<resource>
I have no problem getting the menu to show as you’ve described, but the breadcrumb trail does not show. No errors. Nothing in the source HTML.
Scratching my head and would really appreciate some help debugging this.
Will happily send more details if someone is still responding to this thread
Hi Marc,
We’d like to see more details about your problem.
Regards,
Burn
Burn —
I ultimately was able to sort it out by changing my XML file to use the format:
somecontroller
someaction
For some reason, the /index/about format is a problem.
Probably related to this is that this method:
$uri = $this->_request->getPathInfo();
$activeNav = $this->view->navigation()->findByUri($uri);
$activeNav->active = true;
$activeNav->setClass(”active”);
returns the error:
Call to undefined method stdClass::setClass()
I believe I read somewhere that 1.9 includes setting the ‘active’ class.
So now wondering if this is somehow related to my 1.9 upgrade. Maybe doesn’t support the format?
I can send along the specifics of my bootstrap and ini files, if you think this is something that should still work in 1.9, otherwise I’m happy to plod forward with my new xml.
Thanks much for responding!
Marc
I see my tag signs were mangled…
controller
action
works.
/index/about
didn’t.
Nice to hear it’s already working. I just followed Jon’s video tutorial to the dot:
// controller
$uri = $this->_request->getPathInfo();
$activeNav = $this->view->navigation()->findByUri($uri);
$activeNav->active = true;
// view
$breadcrumbs = $this->navigation() ->breadcrumbs() ->setMinDepth(0)
->setLinkLast(true)
->setSeparator(’ : ‘);
and yeah, I use 1.9.3.pl.1 and having /user/editprofile causes no problem. May be it got fixed in 1.9.3.pl.1.
Regards,
Burn
I tracked the problem to a simple resource plugin that I was experimenting with a few days ago. Being new to ZF, I’m wondering if you can tell me why it caused a problem:
class Mac_Resource_Plugin_Testview extends Zend_Application_Resource_View
{
public function init()
{
parent::init();
$view = $this->getView();
$view->headTitle()->setSeparator(’…’);
}
}
I know there is some issue with the view object, but not clear why it conflicts with the navigation scheme.
Hi Marc,
Hmm…. I don’t think headlink has a separator.
That’s headTitle() I’m setting the separator for.
M
[...] Zend_Navigation – creating a menu, a sitemap and breadcrumbs | free Zend Framework screencasts …. This entry was posted in PHP, Server Side, Zend Framework and tagged PHP, Zend Framework. [...]
hi,
I would like to implment the breadcrumbs. They work except when i have an ID in the URL like
http://www.daily-sudoku.com/dailysudoku/play-sudoku-online/solve-online-207.html
now i do not know how to represent this in the navigation.xml.
any ideas?
Sudoku –
I’m new to the framework but try using this structure for adding parameters to your xml file:
1
2
That didn’t come through. Maybe this will…
<params>
<foo>1</foo>
<bar>2</bar>
</params>
hi,
I can’t use findByUri to achieve the function, do you help me ?
My question write at:
http://www.zendcasts.com/forum/post/284/#p284
please help me, thanks.
hey,
for those of you having problems with empty breadcrumbs and sitemap in zf 1.9.x – just add the following line after creating the Zend_Navigation Object in your Bootstrap.php:
Zend_Registry::set(Zend_Navigation, $yourNavObj);
great tutorial btw
regards from germany
Hi Rock,
You could try and start out with this:
http://framework.zend.com/manual/en/zend.navigation.containers.html
In my site, i.e. footer, I use findByLabel:
$aMenus = array(’User’, ‘Tools’, ‘Account’, ‘Help’);
for ($i = 0; $i \navigation()-\>findOneByLabel($aMenus[$i]);
…
Regards,
clybs/burn
I have a question though, how do you place a rel=nofollow in the nav xml?
Hey am getting this error
“Uncaught exception ‘Zend_Navigation_Exception’ with message ‘Invalid argument: Unable to determine class to instantiate’ ”
How do i go about it?
What about updating when using ajax calls?
Hi, I don’t think all that stuff in controller init() is necesary since you can drop in your config.xml and use like this:
Logout
default
authentication
logout
Login
default
authentication
login
this way the breadcrumbs automatically know where they are and active pages are automatically assigned. Plus it stays functional even when u change structure of your urls..
For more info: http://framework.zend.com/manual/en/zend.navigation.pages.html#zend.navigation.pages.mvc
P.S.: you can also hide links in menu that aren’t normally available. IE you dont want your guests to see links to the administration area. You will have to have ACL and Auth running which is for another quite HUGE tutorial..
Oops, it deleted all the xml markup… So I’m gonna try again, sorry for double post..
Hi, I don’t think all that stuff in controller init() is necesary since you can drop <uri> in your config.xml and use <module><controller><action> like this:
<?xml version=”1.0″ encoding=”utf-8″?>
<config>
<nav>
<logout>
<label>Logout</label>
<module>default</module>
<controller>authentication</controller>
<action>logout</action>
</logout>
<login>
<label>Login</label>
<module>default</module>
<controller>authentication</controller>
<action>login</action>
</login>
</nav>
<config>
this way the breadcrumbs automatically know where they are and active pages are automatically assigned. Plus it stays functional even when u change structure of your urls..
For more info: http://framework.zend.com/manual/en/zend.navigation.pages.html#zend.navigation.pages.mvc
P.S.: you can also hide links in menu that aren’t normally available. IE you dont want your guests to see links to the administration area. You will have to have ACL and Auth running which is for another quite HUGE tutorial..
How do you get this thing to work, if you use production and development sections in your navigation.xml??
[configdata]
[production]
[nav]
[home]
…
[/home]
[/nav]
[/production]
[development extends="production"]
[/development]
[/configdata]
If i leave the sections out, it complaints it can not find the production section.
If i specify the ‘nav’ section in bootstrap, it complaints it can not find the nav section.
$config = new Zend_Config_Xml(APPLICATION_PATH.’/configs/navigation.xml’, ‘nav’);
Found nothing on the web that relates to this problem, is no one using these build sections???
[...] Sitemaps can quickly and easily be generated using Zend_Navigation, a great quick tutorial (and generally very useful for Zend Framework tutorials) is Zend Casts – Dynamically creating a menu a sitemap and breadcrumbs. [...]
Very cool dude. Thanks.
Anybody out there who can explain how to integrate Zend-Translate into a menu defined in the navigation.xml?