add('news', 'Neuigkeiten', '/neuigkeiten', active: 'news.*', sort: 20); */ class Navigation { /** @var array, sort: int, ability: string|null}> */ private array $items = []; public function __construct() { $this->add('home', 'Startseite', '/', active: 'dashboard', sort: 0); $this->add('people', 'Personen', '/personen', active: 'people.*', sort: 90); } /** * @param string|list $active Routennamen (mit *), bei denen der Eintrag aktiv ist */ public function add(string $key, string $label, string $href, string|array $active = [], int $sort = 50, ?string $ability = null): void { $this->items[$key] = ['label' => $label, 'href' => $href, 'active' => (array) $active, 'sort' => $sort, 'ability' => $ability]; } public function remove(string $key): void { unset($this->items[$key]); } /** * @return list */ public function items(Request $request, ?User $user = null): array { $items = collect($this->items) ->filter(fn (array $item) => $item['ability'] === null || ($user?->can($item['ability']) ?? false)) ->sortBy('sort') ->map(fn (array $item, string $key) => [ 'key' => $key, 'label' => $item['label'], 'href' => $item['href'], 'active' => $item['active'] !== [] && $request->routeIs(...$item['active']), ]) ->values() ->all(); return Hooks::apply('navigation.items', $items, $request, $user); } }