Skip to content

Commit 377a2ba

Browse files
committed
Create new documentation for simplified API
1 parent 15b8b65 commit 377a2ba

File tree

2 files changed

+55
-126
lines changed

2 files changed

+55
-126
lines changed

docs/advanced-features/navigation-api.md

Lines changed: 22 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -155,120 +155,45 @@ The `NavigationMenu` class represents a navigation menu. It contains a collectio
155155

156156
## NavigationItem
157157

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.
159-
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.
161-
162-
```php
163-
use Hyde\Framework\Features\Navigation\NavigationItem;
164-
165-
$item = new NavigationItem(Route|string $destination, string $label, int $priority = 500);
166-
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.
172-
```
158+
The `NavigationItem` class is an abstraction for a navigation menu item containing useful information like the destination, label, and priority.
173159

174160
### Creating Navigation Items
175161

176-
There are several ways to create navigation items using the `NavigationItem` class.
177-
178-
#### Direct instantiation
179-
180-
You can create instances directly by passing either a URL or a Route instance to the constructor.
181-
182-
The first parameter is the destination, the second is the label, and the third optional parameter is the priority.
183-
184-
```php
185-
$item = new NavigationItem('index', 'Home');
186-
$item = new NavigationItem(Routes::get('index'), 'Home');
187-
$item = new NavigationItem('https://example.com', 'External Link');
188-
```
189-
190-
#### Setting Priority
162+
There are two syntaxes for creating NavigationItem instances, you can use a standard constructor or the static create method.
163+
Both options provide the exact same signature and functionality, so it's just a matter of preference which one you use.
191164

192-
You can set the priority of the item, which determines its position in the menu.
165+
The constructors take three parameters: the destination, the label, and the optional priority.
166+
The destination can be a `Route` instance, a route key string, or an external URL.
193167

194168
```php
195-
$item = new NavigationItem(Routes::get('index'), 'Home', 25);
196-
```
197-
198-
#### Static Creation Method
199-
200-
You can use the static `create` method, which can automatically fill in the label and priority from a Route.
201-
202-
```php
203-
$item = NavigationItem::create(Routes::get('index'));
204-
```
205-
206-
You can also pass a custom label and priority to the `create` method to override the defaults.
207-
208-
```php
209-
$item = NavigationItem::create(Routes::get('index'), 'Custom Label', 50);
210-
```
211-
212-
#### Using Route Keys and URLs
213-
214-
The `create` method works with route keys and URLs.
215-
216-
```php
217-
$item = NavigationItem::create('index');
218-
$item = NavigationItem::create('https://example.com');
219-
```
220-
221-
Unless you pass a custom label to URL items, the label will be the URL itself.
222-
223-
```php
224-
$item = NavigationItem::create('https://example.com');
225-
```
226-
227-
### Navigation Item Methods
228-
229-
### Getting Label and Link
230-
231-
```php
232-
$item = NavigationItem::create('index');
233-
234-
// Get the label of the item.
235-
$item->getLabel(); // Returns 'Home'
236-
237-
// Get the link of the item.
238-
$item->getLink(); // Returns 'index.html'
239-
```
240-
241-
You can also get the link by casting the item to a string.
169+
use Hyde\Framework\Features\Navigation\NavigationItem;
242170

243-
```php
244-
(string) $item; // Returns 'index.html'
171+
$item = new NavigationItem($destination, $label, $priority);
172+
$item = NavigationItem::create($destination, $label, $priority);
245173
```
246174

247-
### Getting Priority
248-
249-
```php
250-
$item->getPriority(); // Returns 0
251-
```
175+
#### Using Routes
252176

253-
### Getting Page Instance
177+
Using the HydePHP routing system is the recommended way to create navigation items leading to pages within your project,
178+
as they will automatically have links resolved to the correct URL, and Hyde can check if the items are active.
179+
Additionally, Hyde will use the page data as the label and priority defaults unless you override them.
254180

255-
You can get the underlying Page instance of the item, if it exists.
181+
Create routed navigation items by providing either a `Route` instance or a route key string as the destination.
256182

257183
```php
258-
$item->getPage(); // Returns instance of BladePage or null
259-
```
260-
261-
### Checking Active State
184+
// Using a Route instance will automatically fill in the label and priority from the route's connected page.
185+
$item = new NavigationItem(Routes::get('index'));
186+
// ['destination' => 'index.html', 'label' => 'Home', 'priority' => 0]
262187

263-
You can check if the item is active (i.e., the current page being rendered).
188+
// Using a route key provides the same functionality as using a Route instance.
189+
// Make sure the route exists otherwise it will be treated as a link.
190+
$item = new NavigationItem('index'); // Exactly the same as above, but without type safety.
264191

265-
```php
266-
$item->isActive(); // Returns false
192+
// Setting the label and/or priorities will override the page's data.
193+
$item = new NavigationItem(Routes::get('index'), 'Custom Label', 10);
194+
// ['destination' => 'index.html', 'label' => 'Custom Label', 'priority' => 10]
267195
```
268196

269-
This concludes the documentation for the `NavigationItem` class.
270-
271-
272197
## NavigationGroup
273198

274199
The `NavigationGroup` class represents a group of items in a navigation menu. It contains a label, priority, and a collection of navigation items.

packages/framework/tests/Feature/NavigationAPITest.php

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -51,44 +51,37 @@ public function testServiceContainerMenus()
5151

5252
public function testNavigationItems()
5353
{
54-
// Each navigation item is represented by a NavigationItem instance containing the needed information.
54+
// The NavigationItem class is an abstraction for a navigation menu item containing useful information like the destination, label, and priority.
5555

5656
// ## Creating Navigation Items
5757

58-
// There are a few ways to create navigation items, depending on your needs.
58+
// There are two syntaxes for creating NavigationItem instances, you can use a standard constructor or the static create method.
59+
// Both options provide the exact same signature and functionality, so it's just a matter of preference which one you use.
5960

60-
// You can create instances directly by passing either a URL or Route instance to the constructor.
61-
$item = new NavigationItem(Routes::get('index'), 'Home');
62-
$this->assertInstanceOf(NavigationItem::class, $item);
63-
$item = new NavigationItem('https://example.com', 'External Link');
64-
$this->assertInstanceOf(NavigationItem::class, $item);
61+
// The constructors take three parameters: the destination, the label, and the optional priority.
62+
// The destination can be a Route instance, a route key string, or an external URL.
6563

66-
// You can also set the priority of the item, which determines its position in the menu.
67-
$item = new NavigationItem(Routes::get('index'), 'Home', 25);
68-
$this->assertSame(25, $item->getPriority());
64+
// Using a Route instance will automatically fill in the label and priority from the route's connected page.
65+
$item = new NavigationItem(Routes::get('index'));
66+
$this->assertData(['destination' => 'index.html', 'label' => 'Home', 'priority' => 0], $item);
6967

70-
// You can also create instances using the static create method, which can automatically fill in the label and priority from a Route.
71-
$item = NavigationItem::create(Routes::get('index'));
72-
$this->assertSame('Home', $item->getLabel());
73-
$this->assertSame(0, $item->getPriority());
68+
// Using a route key provides the same functionality as using a Route instance.
69+
// Make sure the route exists otherwise it will be treated as a link.
70+
$item = new NavigationItem('index');
71+
$this->assertEquals(new NavigationItem(Routes::get('index')), $item);
7472

75-
// You can also pass a custom label and priority to the create method to override the defaults.
76-
$item = NavigationItem::create(Routes::get('index'), 'Custom Label', 50);
77-
$this->assertSame('Custom Label', $item->getLabel());
78-
$this->assertSame(50, $item->getPriority());
79-
80-
// The create method also works with route keys.
81-
$item = NavigationItem::create('index');
82-
$this->assertSame('Home', $item->getLabel());
83-
$this->assertEquals(NavigationItem::create('index'), NavigationItem::create(Routes::get('index')));
73+
// Setting the label and/or priorities will override the page's data.
74+
$item = new NavigationItem(Routes::get('index'), 'Custom Label', 10);
75+
$this->assertData(['destination' => 'index.html', 'label' => 'Custom Label', 'priority' => 10], $item);
8476

85-
// You can also pass a URL to the create method.
86-
$item = NavigationItem::create('https://example.com');
87-
$this->assertSame('https://example.com', $item->getLink());
77+
// You can also pass an external URL as the destination.
78+
$item = new NavigationItem('https://example.com', 'External Link', 10);
79+
$this->assertData(['destination' => 'https://example.com', 'label' => 'External Link', 'priority' => 10], $item);
8880

89-
// Unless you pass a custom label to URL items, the label will be the URL itself.
90-
$item = NavigationItem::create('https://example.com');
91-
$this->assertSame('https://example.com', $item->getLabel());
81+
// If you do not set a label for links, the label will default to the URL.
82+
// And if uf you do not set a priority, it will default to 500.
83+
$item = new NavigationItem('https://example.com');
84+
$this->assertData(['destination' => 'https://example.com', 'label' => 'https://example.com', 'priority' => 500], $item);
9285

9386
// ## Navigation Item Methods
9487

@@ -229,4 +222,15 @@ protected function removeIndentation(string $actual): string
229222
{
230223
return implode("\n", array_map('trim', explode("\n", $actual)));
231224
}
225+
226+
protected function assertData(array $expected, NavigationItem $item): void
227+
{
228+
$actual = [
229+
'destination' => $item->getLink(),
230+
'label' => $item->getLabel(),
231+
'priority' => $item->getPriority(),
232+
];
233+
234+
$this->assertSame($expected, $actual);
235+
}
232236
}

0 commit comments

Comments
 (0)