Quick-and-dirty introduction to using Zend\Navigation in Zend Framework 2.
(1) Add Service Manager Factory
Add the following to your module configuration file (
config/module.config.php) or place it in an autoloadable file in your application’s
1 2 3 4 5
This tells ZF2′s Serivce Manager to load the default Zend\Navigation instance factory. This factory will read a page tree from the application configuration and register a new instance of the navigation data container (
Zend\Navigation\Navigation) with the service manager under the name
(2) Configure Sitemap
For each module in your application you will need to construct a sitemap for the routes exposed by that module. As an example, here is one that I use with ZfcUser in my own application:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
(3) Using the View Helpers
Now that you’ve set up the necessary configuration, all that’s left to do is use it! There are a number of bundled view helpers which you can use to inject navigation components into your views.
To render breadcrumbs add the following line to one of your views (I put it in my application’s layout view):
To render a sidebar navigation menu, add the following line to the location in your view script where you want the menu rendered:
This view helper also provides additional methods for altering the output. For example, to display only the active branch of the menu, append the call setOnlyActiveBranch(true) like so: