我的方法是
public function topmenu($parentsid=null){
$this->layout =false;
$category_tree = $this->Categorymaster->find('all',array('order'=>'Categorymaster.lft ASC','conditions'=>array('Categorymaster.parent_id'=>$parentsid)));
echo '<ul class="sub-menu" role="menu">';
foreach($category_tree as $parentval){
echo '<li>'.$parentval['Categorymaster']['name'].'</li>';
$id = $parentval['Categorymaster']['id'];
$haschild = $this->Categorymaster->children($id, true);
if (!empty($haschild)) {
$this->topmenu($id);
}
}
echo '</ul>';
$this->set(compact('category_tree'));
$this->render('topmenu');
}
我从控制器获得输出
我正在尝试foreach
在我的topmenu.ctp文件中使用循环,但是由于cakephp是mvc,因此在行中给出了错误
$haschild = $this->Categorymaster->children($id, true);
if (!empty($haschild)) {
$this->topmenu($id);
}
所以它怎么可以使用topmenu()
.ctp文件中的方法,以便我可以在菜单或任何其他替代方法中显示它。
首先,直接在ctp(视图)文件中调用控制器方法永远不是一个好习惯。
如果您仍然想这样做,请尝试这样拨打电话:-
Controller name::(scope resolution operator) function name(parameters).
注意:-确保您的方法是公开的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句