ZendFramework - Zend_Navigation。 Find* 方法从容器对象中删除页面

发布于 2025-01-01 12:34:43 字数 2624 浏览 0 评论 0原文

当在 Zend_Navigation_Container 页面上使用 findAll 方法时,找到的页面将从容器对象中删除。

即使当我克隆容器对象并且在该克隆对象上调用 findAll 时,也会发生同样的情况。

我不明白这是怎么发生的。

但是,我注意到,只有当我尝试“查找”第二级(或更深层)的页面时,才会发生这种情况

这里似乎描述了同样的问题,没有给出答案...

这是一些代码,用于重现问题:

1.第一种方法,没有克隆,任何“找到”的页面都是从容器 2 中删除

<?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);
?>

。第二次尝试,克隆,“找到”的第二级(及更深)页面已从容器

<?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 中删除。最后一次,克隆,“找到”的第一级(及更深)页面未删除来自容器

与上面相同的代码,仅“第 2 页”代替“第 1.1 页”

有人能告诉我这是怎么回事吗?

我想要实现的只是在两个不同的地方显示相同的菜单。在这两个地方,菜单都是从容器页面的一部分构建的,并使用 findXXX 方法进行过滤...

但是对于所描述的问题,这似乎是不可能的:(

提前感谢您的任何建议。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与往事干杯 2025-01-08 12:34:43

试试这个,你走在正确的轨道上,我只是认为你对 Zend_Navigation 和 navigation() 视图助手之间的差异有困难。

<?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);
?>

就我个人而言,我喜欢在引导程序中设置导航并使用配置文件来创建容器。

 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);
    }

希望这有帮助:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文