how to implement breadcrumbs using Zend_Navigation

farzad picture farzad · Jul 11, 2009 · Viewed 9.2k times · Source

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?

Answer

Stefan Gehrig picture Stefan Gehrig · Jul 11, 2009

Getting breadcrumbs is quite easy:

  • register your Zend_Navigation object that you created in your bootstrap (or some other place) in the Zend_Registry with key Zend_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 the Zend_Application_Resource_Navigation resource to setup navigation. Just set resources.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.