Skip to content

Commit f9a1d58

Browse files
committed
feat(docs): Enhance README with navigation examples and customization details
Add sections for fetching navigation, asynchronous navigation requests, and customizing navigation requests in the README. Update example files to reflect changes in navigation item access and improve clarity on usage.
1 parent 161cfd8 commit f9a1d58

3 files changed

Lines changed: 207 additions & 133 deletions

File tree

README.md

Lines changed: 185 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,32 @@ try {
9090
}
9191
```
9292

93+
### Fetching Navigation
94+
95+
```php
96+
try {
97+
// Create a navigation request
98+
$navRequest = $client->createNavigationRequest('/about-us', 2);
99+
100+
// Get the navigation
101+
$nav = $client->getNavigation($navRequest);
102+
103+
// Access navigation information
104+
echo "Navigation title: " . $nav['title'] . "\n";
105+
echo "Navigation URL: " . $nav['href'] . "\n";
106+
107+
// Access children if available
108+
if ($nav->hasChildren()) {
109+
foreach ($nav->getChildren() as $child) {
110+
echo "- " . $child['title'] . " (" . $child['href'] . ")\n";
111+
}
112+
}
113+
114+
} catch (\Exception $e) {
115+
echo "Error: " . $e->getMessage() . "\n";
116+
}
117+
```
118+
93119
### Asynchronous Requests
94120

95121
```php
@@ -113,6 +139,34 @@ $promise->then(
113139
$promise->wait();
114140
```
115141

142+
### Asynchronous Navigation Requests
143+
144+
```php
145+
// Create a navigation request
146+
$asyncNavRequest = $client->createNavigationRequest('/', 2);
147+
148+
// Get the navigation asynchronously
149+
$promise = $client->getNavigationAsync($asyncNavRequest);
150+
151+
// Add callbacks for success and failure
152+
$promise->then(
153+
function ($nav) {
154+
echo "Navigation title: " . $nav['title'] . "\n";
155+
if ($nav->hasChildren()) {
156+
foreach ($nav->getChildren() as $child) {
157+
echo "- " . $child['title'] . "\n";
158+
}
159+
}
160+
},
161+
function (\Exception $e) {
162+
echo "Error: " . $e->getMessage() . "\n";
163+
}
164+
);
165+
166+
// Wait for the promise to complete
167+
$promise->wait();
168+
```
169+
116170
## Advanced Usage
117171

118172
### Customizing Page Requests
@@ -143,6 +197,22 @@ $pageRequest = $pageRequest->withHostId('48190c8c-42c4-46af-8d1a-0cd5db894797');
143197

144198
Note that each method returns a new instance with the updated value, so you need to reassign the result.
145199

200+
### Customizing Navigation Requests
201+
202+
The `NavigationRequest` class allows you to customize your navigation requests:
203+
204+
```php
205+
// Create a navigation request with custom parameters
206+
$navRequest = $client->createNavigationRequest(
207+
path: '/about-us', // The root path to begin traversing
208+
depth: 2, // The depth of the folder tree to return (1-3)
209+
languageId: 2 // The language ID for content (e.g., 2 for Spanish)
210+
);
211+
212+
// Get the navigation with the custom parameters
213+
$nav = $client->getNavigation($navRequest);
214+
```
215+
146216
### Working with Page Components
147217

148218
Once you have a page, you can access its components:
@@ -179,6 +249,43 @@ foreach ($page->containers as $containerId => $container) {
179249
}
180250
```
181251

252+
### Working with Navigation Items
253+
254+
The `NavigationItem` class extends `AbstractModel` and provides array access to its properties:
255+
256+
```php
257+
// Check if the navigation item is a folder
258+
if ($nav->isFolder()) {
259+
echo "This is a folder\n";
260+
}
261+
262+
// Check if the navigation item is a page
263+
if ($nav->isPage()) {
264+
echo "This is a page\n";
265+
}
266+
267+
// Access navigation properties using array access
268+
echo "Title: " . $nav['title'] . "\n";
269+
echo "URL: " . $nav['href'] . "\n";
270+
echo "Type: " . $nav['type'] . "\n";
271+
echo "Target: " . $nav['target'] . "\n"; // e.g., "_self", "_blank"
272+
echo "Order: " . $nav['order'] . "\n";
273+
274+
// Recursively process navigation tree
275+
function processNavigation($navItem, $level = 0) {
276+
$indent = str_repeat(" ", $level);
277+
echo $indent . "- " . $navItem['title'] . " (" . $navItem['href'] . ")\n";
278+
279+
if ($navItem->hasChildren()) {
280+
foreach ($navItem->getChildren() as $child) {
281+
processNavigation($child, $level + 1);
282+
}
283+
}
284+
}
285+
286+
processNavigation($nav);
287+
```
288+
182289
## API Reference
183290

184291
### DotCMSClient
@@ -191,6 +298,9 @@ The main client for interacting with the dotCMS API.
191298
| `getPage` | Fetch a page synchronously | `PageRequest $request` |
192299
| `getPageAsync` | Fetch a page asynchronously | `PageRequest $request` |
193300
| `createPageRequest` | Create a new page request | `string $pagePath, string $format = 'json'` |
301+
| `getNavigation` | Fetch navigation items synchronously | `NavigationRequest $request` |
302+
| `getNavigationAsync` | Fetch navigation items asynchronously | `NavigationRequest $request` |
303+
| `createNavigationRequest` | Create a new navigation request | `string $path = '/', int $depth = 1, int $languageId = 1` |
194304

195305
### PageRequest
196306

@@ -208,6 +318,19 @@ Represents a request to the dotCMS Page API.
208318
| `buildPath` | **(Internal)** Build the API path for the request | None |
209319
| `buildQueryParams` | **(Internal)** Build the query parameters for the request | None |
210320

321+
### NavigationRequest
322+
323+
Represents a request to the dotCMS Navigation API.
324+
325+
| Method | Description | Parameters |
326+
|--------|-------------|------------|
327+
| `__construct` | Create a new navigation request | `string $path = '/', int $depth = 1, int $languageId = 1` |
328+
| `getPath` | Get the path | None |
329+
| `getDepth` | Get the depth | None |
330+
| `getLanguageId` | Get the language ID | None |
331+
| `buildPath` | **(Internal)** Build the API path for the request | None |
332+
| `buildQueryParams` | **(Internal)** Build the query parameters for the request | None |
333+
211334
### PageAsset
212335

213336
Represents a complete page asset from dotCMS.
@@ -220,6 +343,30 @@ Represents a complete page asset from dotCMS.
220343
| `layout` | Layout | The Layout object |
221344
| `containers` | Array | Array of Container objects |
222345

346+
### NavigationItem
347+
348+
Represents a navigation item from the dotCMS Navigation API. Extends AbstractModel to provide array access to properties.
349+
350+
| Property | Type | Description |
351+
|----------|------|-------------|
352+
| `code` | ?string | The code of the navigation item |
353+
| `folder` | ?string | The folder identifier |
354+
| `host` | string | The host identifier |
355+
| `languageId` | int | The language ID |
356+
| `href` | string | The URL of the navigation item |
357+
| `title` | string | The title of the navigation item |
358+
| `type` | string | The type of the navigation item (folder, htmlpage, etc.) |
359+
| `hash` | int | The hash of the navigation item |
360+
| `target` | string | The target attribute for links (_self, _blank, etc.) |
361+
| `order` | int | The order of the navigation item |
362+
363+
| Method | Description | Return Type |
364+
|--------|-------------|-------------|
365+
| `isFolder` | Check if this navigation item is a folder | `bool` |
366+
| `isPage` | Check if this navigation item is a page | `bool` |
367+
| `hasChildren` | Check if this navigation item has children | `bool` |
368+
| `getChildren` | Get the children as NavigationItem objects | `?array` |
369+
223370
## Error Handling
224371

225372
The SDK provides several exception classes for error handling:
@@ -252,7 +399,7 @@ try {
252399

253400
## Examples
254401

255-
### Basic Example
402+
### Basic Page Example
256403

257404
```php
258405
<?php
@@ -282,6 +429,43 @@ echo "Page title: " . $page->page->title . "\n";
282429
echo "Page URL: " . $page->page->pageUrl . "\n";
283430
```
284431

432+
### Basic Navigation Example
433+
434+
```php
435+
<?php
436+
437+
require_once __DIR__ . '/vendor/autoload.php';
438+
439+
use Dotcms\PhpSdk\Config\Config;
440+
use Dotcms\PhpSdk\DotCMSClient;
441+
442+
// Create configuration
443+
$config = new Config(
444+
host: 'https://demo.dotcms.com',
445+
apiKey: 'YOUR_API_KEY'
446+
);
447+
448+
// Create client
449+
$client = new DotCMSClient($config);
450+
451+
// Create navigation request for the About Us section with depth=2
452+
$navRequest = $client->createNavigationRequest('/about-us', 2);
453+
454+
// Get navigation
455+
$nav = $client->getNavigation($navRequest);
456+
457+
// Display navigation information
458+
echo "Navigation title: " . $nav['title'] . "\n";
459+
460+
// Display children if available
461+
if ($nav->hasChildren()) {
462+
echo "Children:\n";
463+
foreach ($nav->getChildren() as $child) {
464+
echo "- " . $child['title'] . " (" . $child['href'] . ")\n";
465+
}
466+
}
467+
```
468+
285469
### Integration with Symfony
286470

287471
The SDK can be easily integrated with Symfony. See the `examples/dotcms-symfony` directory for a complete example.

examples/navigation_example.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@
1919
echo "--------------------------------\n";
2020
$navRequest = $client->createNavigationRequest('/', 1);
2121
$nav = $client->getNavigation($navRequest);
22-
echo "Title: " . $nav->getTitle() . "\n";
23-
echo "URL: " . $nav->getHref() . "\n";
24-
echo "Type: " . $nav->getType() . "\n\n";
22+
echo "Title: " . $nav['title'] . "\n";
23+
echo "URL: " . $nav['href'] . "\n";
24+
echo "Type: " . $nav['type'] . "\n\n";
2525

2626
// Example 2: Get navigation with children (depth=2)
2727
echo "Example 2: Navigation with children (depth=2)\n";
2828
echo "------------------------------------------\n";
2929
$navWithChildrenRequest = $client->createNavigationRequest('/about-us', 2);
3030
$navWithChildren = $client->getNavigation($navWithChildrenRequest);
31-
echo "Title: " . $navWithChildren->getTitle() . "\n";
32-
echo "URL: " . $navWithChildren->getHref() . "\n";
33-
echo "Type: " . $navWithChildren->getType() . "\n";
31+
echo "Title: " . $navWithChildren['title'] . "\n";
32+
echo "URL: " . $navWithChildren['href'] . "\n";
33+
echo "Type: " . $navWithChildren['type'] . "\n";
3434

3535
if ($navWithChildren->hasChildren()) {
3636
echo "Children:\n";
3737
foreach ($navWithChildren->getChildren() as $child) {
38-
echo "- " . $child->getTitle() . " (" . $child->getHref() . ")\n";
38+
echo "- " . $child['title'] . " (" . $child['href'] . ")\n";
3939
}
4040
}
4141
echo "\n";
@@ -45,21 +45,21 @@
4545
echo "-------------------------------------------\n";
4646
$navSpanishRequest = $client->createNavigationRequest('/', 1, 2);
4747
$navSpanish = $client->getNavigation($navSpanishRequest);
48-
echo "Title: " . $navSpanish->getTitle() . "\n";
49-
echo "URL: " . $navSpanish->getHref() . "\n";
50-
echo "Language ID: " . $navSpanish->getLanguageId() . "\n\n";
48+
echo "Title: " . $navSpanish['title'] . "\n";
49+
echo "URL: " . $navSpanish['href'] . "\n";
50+
echo "Language ID: " . $navSpanish['languageId'] . "\n\n";
5151

5252
// Example 4: Async navigation request
5353
echo "Example 4: Async navigation request\n";
5454
echo "--------------------------------\n";
5555
$asyncRequest = $client->createNavigationRequest('/about-us', 2);
5656
$client->getNavigationAsync($asyncRequest)->then(
5757
function ($nav) {
58-
echo "Title: " . $nav->getTitle() . "\n";
58+
echo "Title: " . $nav['title'] . "\n";
5959
if ($nav->hasChildren()) {
6060
echo "Children:\n";
6161
foreach ($nav->getChildren() as $child) {
62-
echo "- " . $child->getTitle() . "\n";
62+
echo "- " . $child['title'] . "\n";
6363
}
6464
}
6565
}

0 commit comments

Comments
 (0)