Skip to content

Commit 86ac424

Browse files
committed
feat(controller): Add dotCMS SDK integration in the AppController
- Updated index method to fetch page and navigation data from dotCMS API. - Added error logging for API exceptions. - Modified view to dynamically set the page title based on dotCMS data.
1 parent 28ebcd4 commit 86ac424

2 files changed

Lines changed: 46 additions & 10 deletions

File tree

examples/dotcms-laravel/app/Http/Controllers/AppController.php

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,57 @@
33
namespace App\Http\Controllers;
44

55
use Illuminate\Http\Request;
6+
use Illuminate\Support\Facades\Log;
7+
use Dotcms\PhpSdk\Config\Config;
8+
use Dotcms\PhpSdk\DotCMSClient;
69

710
class AppController extends Controller
811
{
912
/**
10-
* Handle the SPA rendering
13+
* Handle the SPA rendering with dotCMS page data
1114
*
15+
* @param \Illuminate\Http\Request $request
1216
* @return \Illuminate\View\View
1317
*/
14-
public function index()
18+
public function index(Request $request)
1519
{
16-
// Here you can add your library logic to get data
17-
// $data = YourLibrary::getData();
18-
19-
return view('app');
20+
// Create dotCMS client configuration
21+
$config = new Config(
22+
host: env('DOTCMS_HOST', 'https://demo.dotcms.com'),
23+
apiKey: env('DOTCMS_API_TOKEN', '')
24+
);
25+
26+
// Create dotCMS client
27+
$client = new DotCMSClient($config);
28+
29+
try {
30+
// Get the current path from the request
31+
$path = $request->path();
32+
$path = $path === '/' ? '/' : '/' . $path;
33+
34+
// Create a page request for the current path
35+
$pageRequest = $client->createPageRequest($path);
36+
37+
// Get the page data
38+
$page = $client->getPage($pageRequest);
39+
40+
// Create a navigation request with depth=2
41+
$navRequest = $client->createNavigationRequest('/', 2);
42+
43+
// Get the navigation
44+
$nav = $client->getNavigation($navRequest);
45+
46+
// Pass the data to the view
47+
return view('app', [
48+
'page' => $page,
49+
'navigation' => $nav
50+
]);
51+
} catch (\Exception $e) {
52+
// Log the error
53+
Log::error('dotCMS API Error: ' . $e->getMessage());
54+
55+
// Rethrow the exception to let Laravel handle it
56+
throw $e;
57+
}
2058
}
2159
}

examples/dotcms-laravel/resources/views/app.blade.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
<head>
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6-
<title>DotCMS Laravel</title>
6+
<title>{{ isset($page->page->title) ? $page->page->title : 'DotCMS Laravel' }}</title>
77
<link rel="stylesheet" href="{{ asset('css/app.css') }}">
88
</head>
99
<body>
1010
<div id="app">
11-
<h1>Welcome to DotCMS Laravel</h1>
12-
<p>This is the default view for all routes.</p>
13-
<p>Current URL Path: {{ request()->path() }}</p>
11+
<h1>{{ isset($page->page->title) ? $page->page->title : 'DotCMS Laravel' }}</h1>
1412
</div>
1513
</body>
1614
</html>

0 commit comments

Comments
 (0)