-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppController.php
More file actions
101 lines (85 loc) · 3.22 KB
/
Copy pathAppController.php
File metadata and controls
101 lines (85 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Dotcms\PhpSdk\DotCMSClient;
class AppController extends Controller
{
/**
* The DotCMS client instance.
*/
protected $dotCMSClient;
/**
* Create a new controller instance.
*/
public function __construct(DotCMSClient $dotCMSClient)
{
$this->dotCMSClient = $dotCMSClient;
}
/**
* Handle the SPA rendering with dotCMS page data
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\View\View
*/
public function index(Request $request)
{
try {
// Get the current path from the request
$path = $request->path();
$path = $path === '/' ? '/' : '/' . $path;
// Get query parameters
$languageId = $request->query('language_id');
$mode = $request->query('mode');
$publishDate = $request->query('publishDate');
$personaId = $request->query('personaId');
// Create a page request for the current path
$pageRequest = $this->dotCMSClient->createPageRequest($path, 'json');
// Add query parameters if they exist
if ($languageId) {
$pageRequest = $pageRequest->withLanguageId((int)$languageId);
}
if ($mode) {
$pageRequest = $pageRequest->withMode($mode);
}
if ($personaId) {
$pageRequest = $pageRequest->withPersonaId($personaId);
}
if ($publishDate) {
$pageRequest = $pageRequest->withPublishDate($publishDate);
}
// Get the page data
$pageAsset = $this->dotCMSClient->getPage($pageRequest);
// Create a navigation request with depth=2
$navRequest = $this->dotCMSClient->createNavigationRequest('/', 2);
// Get the navigation
$nav = $this->dotCMSClient->getNavigation($navRequest);
// Check for entity wrapper in the response
if (isset($pageAsset->entity)) {
// Some dotCMS versions return data in an 'entity' wrapper
$page = $pageAsset;
} else {
// Standard structure already expected by our templates
$page = $pageAsset;
}
// Log the structure for debugging
Log::debug('DotCMS Page Structure', [
'hasEntity' => isset($pageAsset->entity) ? 'yes' : 'no',
'hasLayout' => isset($page->layout) ? 'yes' : 'no',
'hasContainers' => isset($page->containers) ? 'yes' : 'no'
]);
// Pass the data to the view
return view('page', [
'pageAsset' => $page,
'navigation' => $nav,
'publishDate' => $publishDate,
'mode' => $mode
]);
} catch (\Exception $e) {
// Log the error
Log::error('dotCMS API Error: ' . $e->getMessage());
// Rethrow the exception to let Laravel handle it
throw $e;
}
}
}