5

Where in my application should I define my top level and lower-level pages for use by Zend Navigation? My top level navigation bar view helper is going to be separate from the view helper that generates the sub navigation.

Chris Laplante
  • 29,338
  • 17
  • 103
  • 134

2 Answers2

3

I usually do this in the controller plugin. Here I can create navigation based on the current route or request parameters, and then easily pass to the view/layout from the application resource and switch with another one, when navigation is redesigned.

takeshin
  • 49,108
  • 32
  • 120
  • 164
3

A simpler way is to define all your navigation in a single place. It supports unlimited nested (child) pages, meaning you can have your main menu as the base level and then the sub-pages under each main page for your sub-menus. Using the View Helpers you can easy output only the sub-page menu for the current active page automatically.

This way keeps all your navigation in a single place, for future maintainability.

For example, I define my site-wide navigation within the application.ini file using the Application Resource, and then within my view scripts use the Navigation View Helpers to format my menus.

This is a small extract from my application.ini file navigation config in a project I am working on:

resources.navigation.pages.exhibits.label                   = "Exhibits"
resources.navigation.pages.exhibits.controller              = "exhibits"
resources.navigation.pages.exhibits.action                  = "index"
resources.navigation.pages.exhibits.pages.index.label       = "Search Exhibitors"
resources.navigation.pages.exhibits.pages.index.controller  = exhibits
resources.navigation.pages.exhibits.pages.index.action      = index
resources.navigation.pages.exhibits.pages.search.label      = "Search Exhibits"
resources.navigation.pages.exhibits.pages.search.controller = exhibits
resources.navigation.pages.exhibits.pages.search.action     = "search"
resources.navigation.pages.exhibits.pages.new.label         = "New Exhibitor"
resources.navigation.pages.exhibits.pages.new.controller    = exhibits
resources.navigation.pages.exhibits.pages.new.action        = "new"
resources.navigation.pages.exhibits.pages.import.label      = "Import Exhibits"
resources.navigation.pages.exhibits.pages.import.controller = exhibits
resources.navigation.pages.exhibits.pages.import.action     = "import"

resources.navigation.pages.sales.label                      = "Sales"
resources.navigation.pages.sales.controller                 = "sales"
resources.navigation.pages.sales.action                     = index
resources.navigation.pages.sales.pages.index.label          = "Review/Search"
resources.navigation.pages.sales.pages.index.controller     = sales
resources.navigation.pages.sales.pages.index.action         = index
resources.navigation.pages.sales.pages.edit.label           = Add/Edit Sales
resources.navigation.pages.sales.pages.edit.controller      = sales
resources.navigation.pages.sales.pages.edit.action          = edit
resources.navigation.pages.sales.pages.flags.label          = Flags/Problems
resources.navigation.pages.sales.pages.flags.controller     = sales
resources.navigation.pages.sales.pages.flags.action         = flags

And within my layout.phtml file:

<div id='mainmenu'>
  <?php echo $this->navigation()->menu()->setMaxDepth(0); ?>
</div> <!-- #mainmenu -->
<div id='submenu'>
  <?php echo $this->navigation()->menu()->setOnlyActiveBranch(true)
                                        ->setMinDepth(1)
                                        ->setMaxDepth(1); ?>
</div> <!-- #submenu -->

So when a user goes to the Exhibits page, they only see the children of that page, and the same with the Sales page. Pretty simple and very effective.

Stephen RC
  • 1,494
  • 2
  • 19
  • 34
  • That's awesome! Thank you! Instead of specifying controllers and actions, can I use route names instead? That way I can simply modify the route and the change will be reflected in the navigation. – Chris Laplante Apr 20 '11 at 14:27
  • I've never used them in Zend_Navigation, but yes you can. The *.route parameter can be used for each page to specify the router to use. See [Zend Navigation Pages MVC](http://framework.zend.com/manual/en/zend.navigation.pages.html#zend.navigation.pages.mvc) for usage. – Stephen RC Apr 20 '11 at 21:04