routers($menus); // 현재 메뉴 위치 조회 $menus = $this->buildMenu($menus, $routers); View::share('menus', $menus); unset($menus, $routers, $menus); return $next($request); } /** * 관리자 메뉴 배열화 */ private function routers(array $menus, $arr = [], $parent = null) { foreach ($menus as $controller => $row) { $info = (key($row) == '/' ? current($row) : $row); if ($parent) { $segment = [ ADMIN_PATH, $parent, $controller ]; } else { $segment = [ ADMIN_PATH, $controller ]; } $arr[$info[1]] = join(DIRECTORY_SEPARATOR, $segment); if (isset($info[5]) && $info[5]) { $arr = $this->routers($info[5], $arr, $controller); } } return $arr; } /** * 관리자 메뉴 생성 */ private function buildMenu(array $menus, array $routers): \Spatie\Menu\Menu|callable { $menu = Menu::new(); foreach ($menus as $row) { $info = (key($row) == '/' ? current($row) : $row); $router = (array_key_exists($info[1], $routers) ? $routers[$info[1]] : ""); try { $link = Link::to(url($router), ( ($info[3] ? Html::raw(sprintf('', $info[3]))->render() : '') . Html::raw(sprintf('%s', $info[2]))->render() ) )->addClass('nav-link')->setActive(function () use($router) { return request()->is($router); }); if (isset($info[5]) && $info[5]) { $id = ('menu_' . $info[1]); $menu->submenu( // 상위 메뉴 $link->setAttributes([ 'data-bs-target' => ('#' . $id), 'data-bs-toggle' => 'collapse', 'role' => 'button', 'aria-expanded' => 'false', 'aria-controls' => $id ])->setActive(function () use($router) { return request()->is($router . '/*'); }), // 하위 메뉴 Menu::new() ->withoutWrapperTag() ->withoutParentTag() ->html( sprintf('
%s
', $id, $this->buildMenu($info[5], $routers)) ) ); }else{ $menu->add( $link ); } } catch (Exception) { exit('관리자 메뉴 설정 값이 잘못되었습니다.'); } } return $menu->addClass('nav flex-column'); } }