Search code examples
phpzend-frameworkzend-navigation

Where should I populate my Zend_Navigation container?


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.


Solution

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