我很难理解为什么这不起作用。我的意思是,我的参考文献错了吗?我还没看到其他一些错误吗?
如果子元素处于活动状态,我想要做的是将父元素设置为活动状态。为了做到这一点,我将对父元素的引用传递给递归函数调用。
但不知怎的,它在实际的菜单树中没有得到更新。仅在局部变量中。
我对其他解决方案持开放态度,但也非常想知道这里出了什么问题。
private static function buildMenuTree($menus, &$parentTreeItem) {
$tree = array();
// loop through every menu item
foreach ($menus as $menu) {
// check if a parent menu exists, if not $parentId will bet set to 0
$parentId = !array_key_exists('menu', $parentTreeItem) ? null : $parentTreeItem['menu']->id;
// initialize new menu entry
$currentTreeItem = [];
// check current menu entry belongs to what was provided to us
if($menu->parent == $parentId) {
//set menu
$currentTreeItem['menu'] = $menu;
// check if route matches, then set the isActive flag
if(Request::is($menu->route)) {
$currentTreeItem['isActive'] = true;
//if there is a valid parent, also set the isActiveFlag on the parent
// TODO: this is changed on the local vaiable but not in the menu tree
if($parentId != null) {
$parentTreeItem['isActive'] = true;
}
} else {
$currentTreeItem['isActive'] = false;
}
// build menu tree for current item
$currentTreeItem['menus'] = self::buildMenuTree($menus, $currentTreeItem);
// add current item to menu tree
$tree[] = $currentTreeItem;
}
}
return $tree;
}
最好的问候