2012-06-27 38 views
3

嘿我想在我的標題,我有全局菜單,然後頁面菜單,然後breadcrumbs佈局。要做到這一點,我主要從這個答案中拿走:Rendering Active Branch of Zend Navigation Without Top LevelZend View Helper導航刪除活動分支,所以麪包屑不會顯示

這很好,除了活動分支從註冊的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> 
... 

回答

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>