You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
>warning This article covers advanced information that is only relevant if you want to create custom navigation menus. Instead, you may want to read the [Navigation](navigation) article for a general overview.
9
9
10
+
10
11
## Abstract
11
12
12
13
This article describes the Navigation API introduced in HydePHP v2. Both the main navigation menu and the documentation sidebar bundled with HydePHP are built with this API.
@@ -16,6 +17,7 @@ This article is intended for advanced users, as most users will not need to crea
16
17
For this reason, the documentation is very code-driven due to the technical nature of the API.
17
18
We'll also be mixing in some practical examples of Blade and PHP code to illustrate how you can use the API in your own projects.
18
19
20
+
19
21
## Overview
20
22
21
23
The Navigation API consists of a set of classes within the `Hyde\Framework\Features\Navigation` namespace.
>info Developer tip: The menus are only generated *after* the Hyde Kernel is booted. If you are getting BindingResolutionExceptions, ensure that you are not trying to access the menus too early in the application lifecycle. (Consider using the `booted` event.)
75
+
>info Developer tip: The menus are only generated *after* the Hyde Kernel is booted. If you are getting BindingResolutionExceptions, ensure that you are not trying to access the menus too early in the application lifecycle. (Consider using the `booted` event.)
76
+
74
77
75
78
## Creating Custom Menus
76
79
@@ -141,135 +144,290 @@ The object-oriented nature of the API also makes this perfect for package develo
141
144
142
145
Here are some general tips to keep in mind when working with the Navigation API:
143
146
- You can use the `add` method to add single items or arrays of items. You can also pass an array of items directly to the menu constructor.
144
-
- The navigation menu items is stored in a Laravel Collection, and is type safe to support both `NavigationItem` and `NavigationGroup` instances.
147
+
- The navigation menu items is stored in a Laravel Collection, and is type safe to support both `NavigationItem` and `NavigationGroup` instances.
145
148
- You can also construct NavigationItem instances directly, but the `create` method is a convenient shorthand, and can fill in data from routes, if you use them.
146
149
- It's also possible to set an item's priority as the third parameter, but here we don't need it, as they default to the order they are added.
147
150
151
+
148
152
## Class Reference
149
153
150
154
Below is a reference of the classes and methods available in the Navigation API.
151
155
156
+
152
157
## NavigationMenu
153
158
154
159
The `NavigationMenu` class represents a navigation menu. It contains a collection of items, which can be either `NavigationItem` or `NavigationGroup` instances.
155
160
156
-
## NavigationItem
157
-
158
-
The `NavigationItem` class represents a single item in a navigation menu. It contains information such as the destination link or route, a label, and priority for ordering in the menu.
161
+
### Quick Reference
159
162
160
-
Here is the constructor signature and a quick reference of the methods available on the `NavigationItem` class, and their types. Keep on reading to see more detailed examples and explanations.
163
+
Here is a quick reference of the methods available on the NavigationMenu class:
161
164
162
165
```php
163
-
use Hyde\Framework\Features\Navigation\NavigationItem;
166
+
use Hyde\Framework\Features\Navigation\NavigationMenu;
164
167
165
-
$item = new NavigationItem(Route|string $destination, string $label, int $priority = 500);
168
+
// Create a new NavigationMenu instance, optionally providing an array of items.
169
+
$menu = new NavigationMenu($items = []);
166
170
167
-
$item->getLink(): string; // Returns the resolved link route or destination URL.
168
-
$item->getLabel(): string; // Returns the label of the item.
169
-
$item->isActive(): bool; // Returns true if the item is the page being rendered.
170
-
$item->getPriority(): int; // Returns the priority of the item.
171
-
$item->getPage(): ?HydePage; // Returns the underlying Page instance, if there is one.
171
+
// Add a single item or an array of items to the menu.
172
+
$menu->add(new NavigationItem());
173
+
$menu->add([new NavigationItem()]);
174
+
175
+
// Get all items in the menu as a sorted Collection.
There are several ways to create navigation items using the `NavigationItem` class.
181
+
You can create a new NavigationMenu instance by simply calling the constructor, optionally providing an array of items.
177
182
178
-
#### Direct instantiation
183
+
```php
184
+
use Hyde\Framework\Features\Navigation\NavigationMenu;
179
185
180
-
You can create instances directly by passing either a URL or a Route instance to the constructor.
186
+
$menu = new NavigationMenu($items = []);
187
+
```
181
188
182
-
The first parameter is the destination, the second is the label, and the third optional parameter is the priority.
189
+
Here is how to provide an array or Collection of `NavigationItem` and/or `NavigationGroup` instances directly to the constructor.
183
190
184
191
```php
185
-
$item = new NavigationItem('index', 'Home');
186
-
$item = new NavigationItem(Routes::get('index'), 'Home');
187
-
$item = new NavigationItem('https://example.com', 'External Link');
192
+
use Hyde\Framework\Features\Navigation\NavigationMenu;
193
+
use Hyde\Framework\Features\Navigation\NavigationItem;
194
+
use Hyde\Framework\Features\Navigation\NavigationGroup;
195
+
196
+
$menu = new NavigationMenu([
197
+
new NavigationItem('index.html', 'Home'),
198
+
new NavigationItem('posts.html', 'Blog'),
199
+
new NavigationGroup('About', [
200
+
new NavigationItem('about.html', 'About Us'),
201
+
new NavigationItem('team.html', 'Our Team'),
202
+
]),
203
+
]);
204
+
```
205
+
206
+
### Adding Items to the Menu
207
+
208
+
You can also add items to the menu after it has been created by using the `add` method which can take a single item or an array of items, and can be fluently chained.
new NavigationItem('privacy.html', 'Privacy Policy'),
215
+
new NavigationItem('terms.html', 'Terms of Service'),
216
+
]);
188
217
```
189
218
190
-
#### Setting Priority
219
+
###Accessing Items in the Menu
191
220
192
-
You can set the priority of the item, which determines its position in the menu.
221
+
You can access all items in the menu by calling the `getItems` method, which will return a `Collection` of all items in the menu.
193
222
194
223
```php
195
-
$item = new NavigationItem(Routes::get('index'), 'Home', 25);
224
+
$items = $menu->getItems();
196
225
```
197
226
198
-
#### Static Creation Method
227
+
The items will automatically be sorted by their priority, with lower numbers coming first, defaulting to the order they were added if no priority is set.
199
228
200
-
You can use the static `create` method, which can automatically fill in the label and priority from a Route.
229
+
## NavigationItem
230
+
231
+
The `NavigationItem` class is an abstraction for a navigation menu item containing useful information like the destination, label, and priority.
232
+
233
+
### Quick Reference
234
+
235
+
Here is a quick reference of the methods available on the `NavigationItem` class:
// This will lead directly to the link, and use it as the label with a priority of 500.
314
+
new NavigationItem('https://example.com');
315
+
316
+
// You can also set a custom label and priority to override the defaults.
317
+
new NavigationItem('https://example.com', 'External Link', 25);
225
318
```
226
319
227
-
### Navigation Item Methods
320
+
While it is discouraged to use external URLs for internal pages, as Hyde won't be able to resolve relative links or check active states,
321
+
they are excellent for any time you want an absolute link to an external site or resource.
228
322
229
-
### Getting Label and Link
323
+
Note that Hyde will not validate or modify the URL, so you are responsible for ensuring it's correct.
324
+
325
+
### Accessing the resolved links
326
+
327
+
The `getLink` method is designed to return a link that can be used in the `href` attribute of an anchor tag.
328
+
329
+
If the destination is a route, the link will be resolved to the correct URL, using relative paths if possible. It will also respect the pretty URL setting.
230
330
231
331
```php
232
-
$item = NavigationItem::create('index');
332
+
$item = new NavigationItem(Routes::get('index'));
333
+
$item->getLink(); // Outputs 'index.html'
233
334
234
-
// Get the label of the item.
235
-
$item->getLabel(); // Returns 'Home'
335
+
$item = new NavigationItem('https://example.com');
**Tip:** The item instances automatically turns into the resolved link when cast to a string. Perfect for your Blade templates!
340
+
341
+
```blade
342
+
<a href="{{ $item }}">{{ $item->getLabel() }}</a>
239
343
```
240
344
241
-
You can also get the link by casting the item to a string.
345
+
### Accessing the label
346
+
347
+
The `getLabel` method returns the label of the item. This is the text that will be displayed in the navigation menu.
242
348
243
349
```php
244
-
(string) $item; // Returns 'index.html'
350
+
$item = new NavigationItem('index', 'Home');
351
+
$item->getLabel(); // Outputs 'Home'
245
352
```
246
353
247
-
### Getting Priority
354
+
### Accessing the priority
355
+
356
+
The `getPriority` method returns the priority of the item. This is a number that determines the order in which the items are displayed in the menu, where lower numbers come first.
248
357
249
358
```php
250
-
$item->getPriority(); // Returns 0
359
+
$item = new NavigationItem('index', 'Home', 25);
360
+
$item->getPriority(); // Outputs 25
251
361
```
252
362
253
-
### Getting Page Instance
363
+
### Checking if the item is active
254
364
255
-
You can get the underlying Page instance of the item, if it exists.
365
+
The `isActive` method checks if the item is active (by comparing it to the Hyde page being compiled at the moment). This is useful for highlighting the current page in the navigation menu.
256
366
257
367
```php
258
-
$item->getPage(); // Returns instance of BladePage or null
368
+
$item = new NavigationItem('index');
369
+
$item->isActive(); // Outputs true if the item is the current page, otherwise false.
259
370
```
260
371
261
-
### Checking Active State
372
+
<!--
373
+
Generated by Copilot, kinda cool, maybe something to implement?
374
+
375
+
### Advanced Usage
376
+
377
+
#### Customizing the Active State
262
378
263
-
You can check if the item is active (i.e., the current page being rendered).
379
+
By default, the `isActive` method will check if the item's destination matches the current page being compiled.
380
+
381
+
However, you can also provide a custom callback to the method to determine if the item is active.
// Get the group key, which is a normalized kebab-case version of the label.
424
+
$group->getGroupKey(): string;
425
+
```
426
+
427
+
As the `NavigationGroup` class extends the `NavigationMenu` class, please see the `NavigationMenu` section for detailed information of the methods available.
428
+
429
+
### Usage Scenarios
430
+
431
+
HydePHP uses the `NavigationGroup` class to create dropdowns in the main navigation menu and the category groups in the documentation sidebar.
432
+
433
+
In your own custom menus, you can use this class for the same types of functionality, and you can even nest groups within groups to create complex navigation structures.
0 commit comments