我有一个自定义类,我存储在/ app / Lib中,我想使用htmlhelper,但由于类没有扩展任何引用,因为$this->Html->link
中的$ this会引发错误:调用到非对象的成员函数link()
如何在我自己的班级中使用这个助手?
是:
<?php
class Tree {
private $level = 0;
public function show_tree($tree_array) {
$this->level++;
$style = ($this->level==1) ? ' class="sortable"':'';
echo "<ol".$style.">\n";
foreach ($tree_array as $t) {
echo "<li id=\"list_".$t['Category']['id']."\">\n";
echo "<div>".$t['Category']['name'];?>
echo $this->Html->link(__('View'), array('action' => 'view', $t['Category']['id']));
echo $this->Html->link(__('Edit'), array('action' => 'edit', $t['Category']['id']));
echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $t['Category']['id']), null, __('Are you sure you want to delete # %s?', $t['Category']['id']));
echo "</span>\n";
echo "</div>\n";
if (!empty($t['children'])) $this->show_tree($t['children']);
echo "</li>\n";
}
echo "</ol>\n";
$this->level--;
}
}
答案 0 :(得分:1)
通过查看代码,您显然需要帮助程序,而不是lib。
扩展html帮助程序或在您的自定义帮助程序中使用它,例如名为NestedListHelper。这是MVC上下文中的正确方法,也是编写代码最少的代码。
看看这个TreeHelper,它还会根据树结构生成嵌套列表,它可能类似于你尝试过的东西:https://github.com/CakeDC/utils/blob/master/View/Helper/TreeHelper.php