dongmangsha7354 2011-12-13 22:32
浏览 58

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

i created custom helper to build dynamic menu and i need to use this helper in all my site pages so i put the code to show menu in element and include it in default.ctp like this

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

,, but the default.ctp not defined the helper so how to define this helper in all views it give me this errors

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]

helpers/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 = "
" . 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;
    }
}
?>    

elements/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条回答 默认 最新

  • dongyang2229 2011-12-14 09:58
    关注

    You have to define it in your AppController, located in the root of your app directory (and if it's not there, just create a file called app_controller.php You can use the file with the same name in the Cake core directory as a template for this file.

    When you have your app_controller, add the following

    var $helpers = array('Tree');
    

    You might want to add some other standard helpers like Html, Form and Javascript in here as well. All the helpers that are in AppController will be available to all of your controllers.

    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数