menu = $menu; } public function generate() { $items = json_decode($this->menu->items, true); if (!empty($items)) { // echo ''; } } public function generateTree($items = [], $depth = 0, $parentKey = '') { // Duyệt qua từng mục trong danh sách foreach ($items as $k => $item) { // Lấy class của mục, nếu không có thì trả về chuỗi rỗng $class = e($item['class'] ?? ''); // Lấy url của mục, nếu không có thì trả về chuỗi rỗng $url = $item['url'] ?? ''; // Lấy target của mục, nếu không có thì trả về chuỗi rỗng $item['target'] = $item['target'] ?? ''; // Nếu không có model cho mục, bỏ qua mục này if (!isset($item['item_model'])) continue; // Nếu model cho mục tồn tại if (class_exists($item['item_model'])) { // Lấy class của model $itemClass = $item['item_model']; // Tìm mục trong database theo id $itemObj = $itemClass::find($item['id']); // Nếu không tìm thấy mục, bỏ qua mục này if (empty($itemObj)) { continue; } // Lấy url chi tiết của mục $url = $itemObj->getDetailUrl(); } // Kiểm tra xem mục hiện tại có phải là mục đang được chọn không if ($this->checkCurrentMenu($item, $url)) { // Nếu đúng, thêm class 'active' vào class của mục $class .= ' active'; // Thêm key của mục vào danh sách các mục đang được chọn $this->activeItems[] = $parentKey; } // Nếu mục có danh sách con if (!empty($item['children'])) { // Bắt đầu buffering output ob_start(); // Tạo cây menu cho danh sách con $this->generateTree($item['children'], $depth + 1, $parentKey . '_' . $k); // Lấy output từ buffer và xóa buffer $html = ob_get_clean(); // Nếu key của mục nằm trong danh sách các mục đang được chọn, thêm class 'active' vào class của mục if (in_array($parentKey . '_' . $k, $this->activeItems)) { $class .= ' active '; } } // Thêm depth vào class của mục $class .= ' depth-' . ($depth); // In ra thẻ li với class tương ứng printf('
  • ', $class); // Nếu mục có danh sách con, thêm icon caret vào tên mục if (!empty($item['children'])) { printf('
  • '; } } protected function checkCurrentMenu($item, $url = '') { if (trim($url, '/') == request()->path()) { return true; } if (!static::$currentMenuItem) return false; if (empty($item['item_model'])) return false; if (is_string(static::$currentMenuItem) and ($url == static::$currentMenuItem or $url == url(static::$currentMenuItem))) { return true; } if (is_object(static::$currentMenuItem) and get_class(static::$currentMenuItem) == $item['item_model'] && static::$currentMenuItem->id == $item['id']) { return true; } return false; } public static function setCurrentMenuItem($item) { static::$currentMenuItem = $item; } public static function getActiveMenu() { return static::$currentMenuItem; }} Class "Modules\Core\Walkers\MenuWalker" not found