doukun0888 2012-06-27 19:35
浏览 19

Zend View Helper Navigation删除活动分支,因此不会显示面包屑

Hey I want a layout in my header where I have the global menu, then the page menu and then breadcrumbs. To do this I, mostly, took away from this answer: Rendering Active Branch of Zend Navigation Without Top Level

This works great, except the active branch gets removed form the Zend_Navigation_Container that is registered.

This is how I do it:

<nav>
<?php 
//Create top level menu
echo $this->navigation()->menu()
        ->setUlClass('main')
        ->setMaxDepth(0)
        ->setIndent(4) 
        . PHP_EOL;

//Set to get the whole active branch
$this->navigation()
        ->setMinDepth(0)
        ->setMaxDepth(0);

//Retrive the active branch
$activeBranchTopLvl = 
    $this->navigation()->findActive(
        $this->navigation()->getContainer());

//Get the 2nd level pages for the active page
$activeBranch = $activeBranchTopLvl['page']->getPages();

$subMenu = new Zend_Navigation();

//This is the line that removes the active branch
$subMenu->setPages($activeBranch);

//Render 2nd level menu
echo $this->navigation()->menu($subMenu)
        ->setUlClass('sub')
        . PHP_EOL;

//Set up for breadcrumbs
$this->navigation()->breadcrumbs()->setMinDepth(0);
$this->navigation()->setMaxDepth(null);
$this->navigation()->breadcrumbs()->setRenderInvisible(true);

?>
</nav>
<p id="breadcrumbs"><?php echo $this->navigation()->breadcrumbs()->render(); ?></p>

The result is that the breadcrumbs are non-existant:

<nav>
    <ul class="main">
        <li class="active">
            <a href="/auth">Auth</a>
        </li>
    </ul>
    <ul class="sub">
        <li class="active">
            <a href="/auth/login">Log In</a>
        </li>
        <li>
            <a href="/auth/logout">Log Out</a>
        </li>
    </ul>
</nav>
<p id="breadcrumbs"></p>

The active branch have been cut off to the top level, here is the Zend_Navigation_Container before its cut:

...
$subMenu = new Zend_Navigation();

print_r($this->navigation()->getContainer()->toArray());

//This is the line that removes the active branch
$subMenu->setPages($activeBranch);

//Render 2nd level menu
...

This Outputs the whole Container:

...
Array
(
    [0] => Array
        (
            [label] => Auth
            [fragment] => 
            [id] => 
            [class] => 
            [title] => 
            [target] => 
            [accesskey] => 
            [rel] => Array
                (
                )

            [rev] => Array
                (
                )

            [order] => 
            [resource] => 
            [privilege] => 
            [active] => 
            [visible] => 1
            [type] => Zend_Navigation_Page_Mvc
            [pages] => Array
                (
                    [0] => Array
                        (
                            [label] => Log In
                            [fragment] => 
                            [id] => 
                            [class] => 
                            [title] => 
                            [target] => 
                            [accesskey] => 
                            [rel] => Array
                                (
                                )

                            [rev] => Array
                                (
                                )

                            [order] => 
                            [resource] => 
                            [privilege] => 
                            [active] => 1
                            [visible] => 1
                            [type] => Zend_Navigation_Page_Mvc
                            [pages] => Array
                                (
                                )

                            [action] => login
                            [controller] => auth
                            [module] => 
                            [params] => Array
                                (
                                )

                            [route] => 
                            [reset_params] => 1
                            [encodeUrl] => 1
                        )

                    [1] => Array
                        (
                            [label] => Log Out
                            [fragment] => 
                            [id] => 
                            [class] => 
                            [title] => 
                            [target] => 
                            [accesskey] => 
                            [rel] => Array
                                (
                                )

                            [rev] => Array
                                (
                                )

                            [order] => 
                            [resource] => 
                            [privilege] => 
                            [active] => 
                            [visible] => 1
                            [type] => Zend_Navigation_Page_Mvc
                            [pages] => Array
                                (
                                )

                            [action] => logout
                            [controller] => auth
                            [module] => 
                            [params] => Array
                                (
                                )

                            [route] => 
                            [reset_params] => 1
                            [encodeUrl] => 1
                        )

                )

            [action] => 
            [controller] => auth
            [module] => 
            [params] => Array
                (
                )

            [route] => 
            [reset_params] => 1
            [encodeUrl] => 1
        )

)
...

This is after I add the pages to the new container:

...
$subMenu = new Zend_Navigation();

//This is the line that removes the active branch
$subMenu->setPages($activeBranch);

print_r($this->navigation()->getContainer()->toArray());

//Render 2nd level menu
...

The Output:

...
Array
(
    [0] => Array
        (
            [label] => Auth
            [fragment] => 
            [id] => 
            [class] => 
            [title] => 
            [target] => 
            [accesskey] => 
            [rel] => Array
                (
                )

            [rev] => Array
                (
                )

            [order] => 
            [resource] => 
            [privilege] => 
            [active] => 
            [visible] => 1
            [type] => Zend_Navigation_Page_Mvc
            [pages] => Array
                (
                )

            [action] => 
            [controller] => auth
            [module] => 
            [params] => Array
                (
                )

            [route] => 
            [reset_params] => 1
            [encodeUrl] => 1
        )

)
...

I don't know why this happens as I create the new container and add the pages to it, it seems they are being removed from the container I have in Zend_Registry under the key Zend_Navigation.

Quickly, if I comment out that line the breadcrumbs works again and the container stays intact, but I lose the 2nd level menu of course:

...
$subMenu = new Zend_Navigation();

//This is the line that removes the active branch
//$subMenu->setPages($activeBranch);

//Render 2nd level menu
...

Output:

...
<nav>
    <ul class="main">
        <li class="active">
            <a href="/auth">Auth</a>
        </li>
    </ul>

</nav>
<p id="breadcrumbs"><a href="/auth">Auth</a> &gt; Log In</p>
...
  • 写回答

1条回答 默认 最新

  • doww38701 2012-07-01 20:41
    关注

    Although not pretty I used a custom function to get the array of Zend_Navigation_Page objects that was returned into an array of arrays which then kept the main navigation container intact so both menu levels and the breadcrumbs work.

    header.phtml:

    <nav id="global">
    <?php 
    //Create top level menu
    echo $this->navigation()->menu()
            ->setUlClass('main navigation')
            ->setMaxDepth(0)
            ->setIndent(4)
            . PHP_EOL;
    
    //Set to get the top level active branch
    $this->navigation()
            ->setMinDepth(0)
            ->setMaxDepth(0)
            ->setRenderInvisible(true)
            ;
    
    //Retrive the active branch
    $activeBranchTopLvl = 
        $this->navigation()->findActive(
            $this->navigation()->getContainer());
    
    //Get the 2nd level pages for the active page
    $activeBranch = $activeBranchTopLvl['page']->getPages();
    
    //create a function to make the array of page objects
    //into an array of arrays, accomplished by modifying it as a reference
    $arrayify = create_function(
            '&$value, $key',
            '$value = $value->toArray();'    
        );
    
    //go through the array with the custom function
    array_walk($activeBranch, $arrayify);
    
    $subMenu = new Zend_Navigation();
    
    //This is the line that removes the active branch
    $subMenu->setPages($activeBranch);
    
    //Render 2nd level menu
    echo $this->navigation()->menu($subMenu)
            ->setUlClass('sub navigation')
            ->setRenderInvisible(false)
            . PHP_EOL;
    
    //Set up for breadcrumbs
    $this->navigation()->breadcrumbs()->setMinDepth(0);
    $this->navigation()->setMaxDepth(null);
    $this->navigation()->breadcrumbs()->setRenderInvisible(true);
    
    ?>
    </nav>
    <p id="breadcrumbs"><?php echo $this->navigation()->breadcrumbs()->render(); ?></p>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器