what are the best practices/suggestions/techniques to implement a breadcrumb for a ZendFramework application using Zend_Navigation? how and where is the best method to define the page hierarchy?
2 Answers
Getting breadcrumbs is quite easy:
- register your
Zend_Navigation
object that you created in your bootstrap (or some other place) in theZend_Registry
with keyZend_Navigation
. That way the object will be caught up by all navigation view-helpers. - if you're using the new
Zend_Application
-style bootstrapping you can simply use theZend_Application_Resource_Navigation
resource to setup navigation. Just setresources.navigation.storage.registry = true
in your configuration. you can then simply
echo $this->navigation()->breadcrumbs()
in your view or layout script.
Talking about how to define the page hierarchy, I'd say that if you have a somehow smaller and more static site, you can simply define the pages within your configuration (when using the new Zend_Application
-bootstrapping-approach):
resources.navigation.pages.home.label = "Home"
resources.navigation.pages.home.action = "index"
resources.navigation.pages.home.controller = "index"
resources.navigation.pages.login.label = "Login"
resources.navigation.pages.login.action = "login"
resources.navigation.pages.login.controller = "users"
resources.navigation.pages.users.label = "Users"
resources.navigation.pages.users.action = "list"
resources.navigation.pages.users.controller = "users"
resources.navigation.pages.users.pages.show.label = "Show"
resources.navigation.pages.users.pages.show.action = "show"
resources.navigation.pages.users.pages.show.controller = "users"
...
Alternatively you could use an extra configuration file or you could build your page hierarchy in a front-controller plugin or an action helper, e.g. if you have a fairly large site structure and don't want to instantiate the whole sitemap on each request. That way you can also insert dynamic pages whose labels for example are dynamically created based on the request parameters.

- 82,642
- 24
- 155
- 189
-
Great answer. We have a controller plugin that generates the navigation container for the whole site, and caches it for subsequent requests. Definitely recommend the config solution for small sites though. – David Snabel-Caunt Jul 11 '09 at 14:57
I made 2 posts on this.
http://blog.ekini.net/2009/05/25/zend-framework-making-the-built-in-breadcrumb-helper-work/
Both are from real world experiences. For me, the XML file was easier to understand.

- 5,263
- 8
- 48
- 77
-
I found using the Zend_Config_XML and an XML file for navigation config is a smooth way to do this. thanks. – farzad Jul 15 '09 at 03:47
-
thanks farzad. Although INIs are shorter to write (no closing tags, etc.), I find the XML file simpler to understand and implement. – wenbert Jul 15 '09 at 09:52