3

グローバル メニュー、ページ メニュー、ブレッドクラムのヘッダーにレイアウトが必要です。これを行うために、私はほとんどの場合、この回答から取り去りました: 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 キーの下にあるコンテナから削除されているようです。

すぐに、その行をコメントアウトすると、ブレッドクラムが再び機能し、コンテナーはそのまま残りますが、もちろん第 2 レベルのメニューは失われます。

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

1 に答える 1

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>
于 2012-07-01T20:41:13.360 に答える