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?
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.
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.