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

时间:2012-06-27 19:35:47

标签: php zend-framework zend-view zend-navigation

嘿我想在我的标题中有一个布局,我有全局菜单,然后是页面菜单,然后是面包屑。要做到这一点,我主要是从这个答案中删除了:Rendering Active Branch of Zend Navigation Without Top Level

这很有用,除了活动分支从已注册的Zend_Navigation_Container中删除。

我就是这样做的:

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

结果是面包屑不存在:

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

活动分支已被切断到顶层,这是剪切前的Zend_Navigation_Container:

...
$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
...

这输出整个容器:

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

)
...

这是在我将页面添加到新容器之后:

...
$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
...

输出:

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

)
...

我不知道为什么会发生这种情况,因为我创建了新容器并将页面添加到它,似乎它们已经从Zend_Registry中的容器中以Zend_Navigation键移除了。

很快,如果我注释掉该行,面包屑再次起作用并且容器保持完整,但我当然失去了第二级菜单:

...
$subMenu = new Zend_Navigation();

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

//Render 2nd level menu
...

输出:

...
<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 个答案:

答案 0 :(得分:0)

虽然不是很好但我使用自定义函数来获取Zend_Navigation_Page对象的数组,这些对象被返回到数组数组中,然后保持主导航容器不变,因此菜单级和面包屑都可以工作。

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>