2011-12-13 4 views
1

은 내가 동적 메뉴를 구축하는 사용자 지정 도우미를 만들고 난이default.ctp 파일에서 사용자 정의 도우미를 정의하는 방법은 무엇입니까? ,,

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

처럼 default.ctp에 내가 요소에 메뉴를 표시하는 코드를 넣어 있도록 내 모든 사이트 페이지에이 도우미를 사용할 필요가 그것을 포함 하지만 default.ctp는

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; 
    } 
} 
?>  
이 오류를 줘 모든보기에이 도우미를 정의하는 방법을 너무 도우미를 정의되지3210 개

요소/당신은 (앱 디렉토리의 루트에있는, 당신의 AppController에서 그것을 정의해야

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

이 튜토리얼의 도우미를 사용하려면 컨트롤러를 만들어야한다고 생각한다. 다음은 작업에 더 적합한 또 다른 자습서입니다. [link] (http://articles.classoutfit.com/2009/11/cakephp-dynamic-navigation-bars/) – lanan

답변

1

menu.ctp과가 아니라면, 단지라는 파일을 생성 app_controller.php 당신은 사용할 수 있습니다 이 파일의 템플릿으로 Cake core 디렉토리에있는 같은 이름의 파일.

당신이 당신의 app_controller이

다음

var $helpers = array('Tree'); 

당신은뿐만 아니라 여기에 HTML, 양식 및 자바 스크립트와 같은 다른 표준 헬퍼를 추가 할 수 있습니다 추가 할 수 있습니다. AppController에있는 모든 도우미는 모든 컨트롤러에서 사용할 수 있습니다.