2011-12-13 48 views
1

我創建的自定義幫助建立動態菜單,我需要,所以我把代碼,以顯示元素菜單使用該助手在我所有的網站頁面,並將其納入default.thtml中這樣如何在default.ctp文件中定義自定義助手?

<?php echo $this->element('menu'); ?> 

,,但default.thtml中沒有定義幫手因此,如何在所有視圖定義這個幫手它給我這個錯誤

Notice (8): Undefined variable: data [APP\views\elements\menu.ctp, line 5] 

Warning (2): Invalid argument supplied for foreach() [APP\views\helpers\tree.php, line 28] 

傭工/ tree.php

<?php 
class TreeHelper extends Helper 
{ 

    var $tab = " "; 
    var $helpers = array('Html'); 

    // Main Function 
    function show($name, $data, $style='') 
    { 
     list($modelName, $fieldName) = explode('/', $name); 
     if ($style=='options') { 
      $output = $this->selecttag_options_array($data, $modelName, $fieldName, $style, 0); 
     } else { 
      //$style=''; 
      $output = $this->list_element($data, $modelName, $fieldName, $style, 0); 
     } 
     return $this->output($output); 
    } 

    // This creates a list with optional links attached to it 
    function list_element($data, $modelName, $fieldName, $style, $level) 
    { 
     $tabs = "\n" . str_repeat($this->tab, $level * 2); 
     $li_tabs = $tabs . $this->tab; 

     $output = $tabs. "<ul>"; 
     foreach ($data as $key=>$val) 
     { 
      $output .= $li_tabs . "<li>".$this->style_print_item($val[$modelName], $modelName, $style); 
      if(isset($val['children'][0])) 
      { 
       $output .= $this->list_element($val['children'], $modelName, $fieldName, $style, $level+1); 
       $output .= $li_tabs . "</li>"; 
      } 
      else 
      { 
       $output .= "</li>"; 
      } 
     } 
     $output .= $tabs . "</ul>"; 
     return $output; 
    } 

    // this handles the formatting of the links if there necessary 
    function style_print_item($item, $modelName, $style='') 
    { 
     switch ($style) 
     { 
      case "link": 
       $output = $this->Html->link($item['name'], "view/".$item['id']); 
      break; 

      case "admin": 
       $output = $item['name']; 
       $output .= $this->Html->link(" edit", "edit/".$item['id']); 
       $output .= " "; 
       $output .= $this->Html->link(" del", "delete/".$item['id']); 
      break; 

      default: 
       $output = $item['name']; 
     } 
    return $output; 
    } 

    // recursively reduces deep arrays to single-dimensional arrays 
    // $preserve_keys: (0=>never, 1=>strings, 2=>always) 
    // Source: http://php.net/manual/en/function.array-values.php#77671 
    function array_flatten($array, $preserve_keys = 1, &$newArray = Array()) 
    { 
      foreach ($array as $key => $child) 
      { 
      if (is_array($child)) 
      { 
        $newArray =& $this->array_flatten($child, $preserve_keys, $newArray); 
      } 
      elseif ($preserve_keys + is_string($key) > 1) 
      { 
        $newArray[$key] = $child; 
      } 
      else 
      { 
        $newArray[] = $child; 
      } 
      } 
      return $newArray; 
    } 

    // for formatting selecttag options into an associative array (id, name) 
    function selecttag_options_array($data, $modelName, $fieldName, $style, $level) 
    { 
     // html code does not work here 
     // tried using " " and it didn't work 
     $tabs = "-"; 

     foreach ($data as $key=>$val) 
     { 
      $output[] = array($val[$modelName]['id'] => str_repeat($tabs, $level*2) . ' ' . $val[$modelName]['name']); 

      if(isset($val['children'][0])) 
      { 
       $output[] = $this->selecttag_options_array($val['children'], $modelName, $fieldName, $style, $level+1); 
      } 
     } 

     $output = $this->array_flatten($output, 2); 
     return $output; 
    } 
} 
?>  

元/ menu.ctp

<!-- This will turn the section name into a link --> 
<h3>Basic hierarchical list with name as link</h3> 
<?php echo $tree->show('Section/name', $data, 'link'); ?> 
+0

如何在menu.ctp中定義$ data? – lanan

+0

我用這個嘖嘖http://bakery.cakephp.org/articles/MrRio/2006/09/24/threaded-lists – user1080247

+0

我認爲要使用本教程中的助手,您將不得不創建一個控制器。這裏是另一個教程,可能更適合您的任務:[鏈接](http://articles.classoutfit.com/2009/11/cakephp-dynamic-navigation-bars/) – lanan

回答

1

你在你的AppController中定義它,它位於應用程序的根目錄下(如果它不存在,只是創建一個名爲app_controller.php您可以使用Cake核心目錄中具有相同名稱的文件作爲此文件的模板。

當你有你app_controller,添加以下

var $helpers = array('Tree'); 

您可能要添加一些其他的標準助手,如HTML,表格和Javascript在這裏。 AppController中的所有助手都將可用於所有控制器。

相關問題