2014-09-04 69 views
0

我有導航欄在我的網站,如:CakePHP的:如何使動態導航條

enter image description here

這裏我的代碼,使其:

 <ul class="breadcrumb"> 
      <li> 
       <i class="icon-home"></i> 
       <a href="index.html">Home</a> 
       <i class="icon-angle-right"></i> 
      </li> 
      <li> 
      <?php echo $this->Html->link($title_for_layout,array('controller'=>'controllers','action'=>'index','full_base'=>true));?> 
      </li> 
     </ul> 

在這張圖片中,我我正在與工作人員控制器。所以我的鏈接是「首頁>職員」。如果我用產品控制器鏈接將「首頁>產品」

  1. 但是,如果我有職員控制器和行動的工作是添加。意味着「員工/添加」。我想在導航欄上顯示它,如「主頁>工作人員>添加」。那麼我該怎麼做?

  2. 如果我已經完成了#1。當前鏈接將爲「首頁>職員>添加」。當我想回到工作人員的索引。我點擊「主頁>職員>添加」中的「職員」。我必須使所述連接部內等

    $這 - > HTML->鏈路(還有$ title_for_layout,陣列( '控制器'=> '人員', '動作'=> '索引')

與職員控制器工作時

此鏈接就可以了。當我改變產品,客戶也將被打破。我怎樣才能做到這一點。

我使用CakePHP 1.3 對不起,我問簡單的事情是這樣的。我是新在Cake PHP中感謝您的幫助和閱讀

回答

1

您需要解決這個問題開始的結構化方式。您需要收集您要在麪包屑中顯示的所有鏈接的數組,然後完成並輸出。嘗試是這樣的:

$links = array(); 
// Add the home URL 
$links[] = array(
    'icon' => 'icon-home', 
    'title' => 'Home', 
    'url' => 'index.html' 
); 

// Add the controller 
$links[] = array(
    'title' => ucwords($this->params['controller']), 
    'url' => $this->Html->url(array('controller' => $this->params['controller'], 'action' => 'index', 'full_base' => true)) 
); 

// Now, conditionally add the next parts where necessary 

$param1 = isset($this->params['pass'][0]) ? $this->params['pass'][0] : null; 
if($param1) { 
    $links[] = array(
     'title' => ucwords($param1), 
     'url' => $this->Html->url(array('controller' => $this->params['controller'], 'action' => $this->action, $param1)) 
    ); 
} 

現在你已經得到包含你要輸出三個環節一個結構數組,這樣你就可以輸出他們很容易這樣的:

<ul class="breadcrumb"> 
    <?php 
    $size = count($links); 
    foreach($links as $i => $link) : ?> 
     <li> 
      <?php 
      // Output icon if it's set 
      if(isset($link['icon'])) 
       echo '<i class="' . $link['icon'] . '"></i>'; ?> 

      // Output the link itself 
      echo $this->Html->link($link['title'], $link['url']); 

      // Output the caret if necessary (it's not the last) 
      if($i < $size - 1) 
       echo '<i class="icon-angle-right"></i>'; 
      ?> 
     </li> 
    <?php endforeach; ?> 
</ul> 

注意:

  1. 您可能希望找到一個更好的辦法來獲得標題你的方法,此刻我只是資本的參數/控制器的名稱。
  2. 我沒有測試過這個,但它應該在理論上工作。
  3. 有可能是一個內置的助手產生面包屑 - 看看。
  4. 人員*(單數)
+0

感謝您的答案,但也許我還不夠好理解。我會研究這個。 – TommyDo 2014-09-04 06:45:46