views:

259

answers:

3

Hi ! First time of here, so please bare with me. I have to following problem, using zend framework, specifically zend_nav to create a reusable menu to be passed in via the layout/layout.phtml page. These are the code fragments in their respective files.

first of in application/configs/navigation.xml,

<configdata>
    <nav>      
         <label>Home</label>
         <controller>index</controller>
         <action>index</action>
         <pages>
             <add>
                 <label>Add</label>
                 <controller>post</controller>
                 <action>add</action>
             </add>
             <login>
                 <label>Admin</label>
                 <controller>login</controller>
                 <action>login</action>
             </login>
         </pages>     
     </nav>
 </configdata>

this is then passed into an object in the Bootstrap.php file(only showing that specific method)

protected function __initNavigation(){
    $this->bootstrap('layout');
    $layout = $this->getResource('layout');
    $view = $layout->getView();
    $config = new Zend_Config_Xml(APPLICATION .'/config/navigation.xml', 'nav');
    $container = new Zend_Navigation($config);
    $view->navigation($container);
}

and then finally in the view layout.phtml, the object should return the menu

 <!-- application/layouts/scripts/layout.phtml -->
<?php echo $this->doctype() ?>
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>        
    <title>Zend Blog !</title>
    <?php echo $this->headLink()->appendStylesheet('/css/global.css')?>
</head>
<body>
    <div id="header">
        <div id="header-logo">
            <b>Blog Me !</b>
        </div>
    </div>
    <div id="menu">            
        <?php echo $this->navigation()->menu() ?>
    </div>
    <div id="content">
    <?php echo $this->layout()->content ?>
    </div>
</body>
</html>

The menu does however not show up when i start up my app in the browser, any ideas as to might have gone wrong, is humbly received.

Thanks in advance Kalle Johansson

A: 

If you did not name the function __initNavigation with two _ underscores on purpose then you probably expected the code to run automatically. For it to run automatically you need to use a single underscore.

Another possible problem is that _initNavigation runs before _initView as Zend goes through these resources in alphabetical order. But then you need not access $view in this code. You can use the Zend_Registry to store the navigation container:

protected function _initNavigation() {
    $config = new Zend_Config_Xml(APPLICATION .'/config/navigation.xml', 'nav');
    $container = new Zend_Navigation($config);
    Zend_Registry::set('Zend_Navigation', $container);
}

The Zend_Navigation registry entry will be used by default by any navigation helper when no container is specified.

Matijs
+1  A: 

i think your code is correct just protected function __initNavigation() u should use just one _ in your initNavigation()

then change __initNavigation() to _initNavigation()

user1400
A: 

Well i`m impressed, really quick answers, the problem had several aspects, first of you where both right about using a single underscore sign, thanks a lot the both of you ! And as i t turned out, i did misspell,

$config = new Zend_Config_Xml(APPLICATION .'/config/navigation.xml', 'nav');

should be,

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

my fault. And finally a miss in the navigation.xml file, inside the - node, there should be nodes surrounding each of the "page" nodes, that is for instance for the home. It should have

   <configdata>
     <nav> 
      <home>     
       <label>Home</label>
        <controller>index</controller>
        <action>index</action>
      </home>

That was it !

Again, thanks a lot for your hints and tips i the right direction.

Sinc Kalle Johansson

Kalle Johansson