如何在default.ctp文件中定义自定义帮助器?

时间:2011-12-13 22:32:17

标签: php cakephp cakephp-1.3 cakephp-1.2

我创建了自定义助手来构建动态菜单,我需要在我的所有网站页面中使用这个帮助器,所以我把代码放在元素中显示菜单并将其包含在default.ctp中,就像这样

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

,,但是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;
    }
}
?>    

元素/ 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'); ?>

1 个答案:

答案 0 :(得分:1)

你必须在AppController中定义它,它位于app目录的根目录中(如果它不在那里,只需创建一个名为app_controller.php的文件你可以在Cake核心中使用同名的文件目录作为此文件的模板。

获得app_controller后,添加以下内容

var $helpers = array('Tree');

您可能还希望在此处添加一些其他标准帮助程序,如Html,Form和Javascript。 AppController中的所有助手都可供所有控制器使用。