ZendFramework - Zend_Navigation. Find* methods removes pages from container object

849 views Asked by At

When using findAll method on Zend_Navigation_Container pages, that are found, are being removed from container object.

Same thing happens, even when I clone container object and findAll is being called on this cloned one.

I can't figure how is it happens.

However, i've noticed that it only happens, when i try to "find" pages on the second level (or deeper)

Same problem seems to be described here, no answer was given...

Here is some code, to reproduce problem:

1. First method, no cloning, any page that is "found" is removed from container

<?php
$container = new Zend_Navigation(array(
    array(
        'label'   => 'Page 1',
        'route'   => 'default',
        'pages'   => array(
            array(
                'label'   => 'Page 1.1',
                'route'   => 'default',
            ),
        )
    ),
    array(
        'label'      => 'Page 2',
        'route'      => 'default',
    ),
    array(
        'label'      => 'Page 3',
        'route'      => 'default',
    )
));
echo $this->navigation($container)->menu()->renderMenu($container);
echo '<br /><hr /><br />';
echo $this->navigation($container)->menu()->renderMenu(new Zend_Navigation($container->findAllByLabel('Page 2')));
echo '<br /><hr /><br />';
echo $this->navigation($container)->menu()->renderMenu($container);
?>

2. Second try, cloning, page on second level (and deeper) that is "found" is removed from container

<?php
$container = new Zend_Navigation(array(
    array(
        'label'   => 'Page 1',
        'route'   => 'default',
        'pages'   => array(
            array(
                'label'   => 'Page 1.1',
                'route'   => 'default',
            ),
        )
    ),
    array(
        'label'      => 'Page 2',
        'route'      => 'default',
    ),
    array(
        'label'      => 'Page 3',
        'route'      => 'default',
    )
));

$container1 = clone $container;
$container2 = clone $container;
$container3 = clone $container;

echo $this->navigation($container1)->menu()->renderMenu($container1);
echo '<br /><hr /><br />';
echo $this->navigation($container2)->menu()->renderMenu(new Zend_Navigation($container2->findAllByLabel('Page 1.1')));
echo '<br /><hr /><br />';
echo $this->navigation($container3)->menu()->renderMenu($container3);
?>

3. Last one, cloning, page on first level (and deeper) that is "found" is NOT removed from container

Same code as above, only 'Page 2' in place of 'Page 1.1'

Can someone tell me what's going on here?

All I want to achieve, is to display same menu in two different places. In both places menu is build from part of container pages, filtered with findXXX method...

But with described problem, it seems to be impossible :(

Thanks in advance for any suggestions.

1

There are 1 answers

1
RockyFord On

Try this, you're on the right track, I just think your having trouble with the difference between Zend_Navigation and navigation() view helper.

<?php
//instantiate Zend_navigation object... This also registers this container to the view      helper
$container = new Zend_Navigation(array(
    array(
        'label'   => 'Page 1',
        'route'   => 'default',
        'pages'   => array(
            array(
                'label'   => 'Page 1.1',
                'route'   => 'default',
            ),
        )
    ),
    array(
        'label'      => 'Page 2',
        'route'      => 'default',
    ),
    array(
        'label'      => 'Page 3',
        'route'      => 'default',
    )
));
//now we use the view helper
echo $this->navigation()->menu()->renderMenu($container);
echo '<br /><hr /><br />';
$label = $this->navigation()->findAllByLabel('Page 2');
echo $this->navigation()->menu()->renderMenu($label);
echo '<br /><hr /><br />';
echo $this->navigation()->menu()->renderMenu($container);
?>

personally I like to set navigation up in the bootstrap and use config files to make containers.

 protected function _initNavigation() {

        $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/site.xml');
        $container = new Zend_Navigation($config);
        $registry = Zend_Registry::getInstance();
        $registry->set('Zend_Navigation', $container);
    }

Hope this helps :)