Search code examples
phpmodel-view-controllerzend-frameworkzend-navigation

Zend_Navigation for displaying different menus in Layout


trying to print out about 3 menus on ZF. Currently I can't even get one out. Not quite sure what's going on and why theres no mention of how to get it working on the manual.

So this is my layout.phtml:

<body>
    <?php echo $this->layout()->nav; ?>
    <?php echo $this->layout()->content; ?>
</body>

Not entirely sure if this is how I'm meant to create the navigation but I plan on changing the routes eventually for localization:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <nav>
        <register>
            <label>Register</label>
            <controller>register</controller>
            <action>index</action>
        </register>
</nav>
</config>

I've got this in my bootstrap:

    protected function _initNavigation() 
{

    // Navigation
    $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'nav');
    $container = new Zend_Navigation($config);

}

Only the content displays... Wanted to be able to have different menu types like... show(topMenu), show(loggedinSideMenu) sort of thing

Any idea? Thanks


Solution

  • Couple of things here...

    First, to display navigation, use the appropriate helper. In your layout file...

    <?php echo $this->navigation()->menu()
        ->renderMenu($zendNavigationContainer) ?>
    

    See http://framework.zend.com/manual/en/zend.navigation.introduction.html and http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.navigation

    Secondly, Zend_Application has a resource plugin for navigation however it's only able to handle one container which doesn't really help you. I'd recommend something like this in your Bootstrap method...

    protected function _initNavigation()
    {
        // get config and create containers as before
    
        // bootstrap layout resource and retrieve it
        $this->bootstrap('layout');
        $layout = $this->getResource('layout');
    
        // add containers as layout properties
        $layout->topMenu = $topMenuContainer;
        $layout->loggedInSideMenu = $sideMenuContainer;
    }
    

    Then, in your layout

    <!-- top menu -->
    <?php echo $this->navigation()->menu()
        ->renderMenu($this->layout()->topMenu) ?>
    
    <!-- side menu -->
    <?php echo $this->navigation()->menu()
        ->renderMenu($this->layout()->loggedInSideMenu) ?>