Skip to content

Commit 28a0ccb

Browse files
committed
feat(dotcms-php): Initialize dotCMS PHP SDK example project
- Added .gitignore to exclude vendor and environment files. - Created composer.json for dependency management with dotCMS PHP SDK. - Implemented basic configuration in dotcms.php for SDK initialization. - Developed entry point in index.php to handle requests and render pages. - Established CSS grid system and layout styles in app.css and layout.css. - Created templates for layout, navigation, and content types to render dynamic content. - Included a comprehensive guide for setting up and using the dotCMS PHP SDK.
1 parent cc7134f commit 28a0ccb

15 files changed

Lines changed: 1063 additions & 0 deletions

File tree

examples/dotcms-php/.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Composer
2+
/vendor/
3+
composer.lock
4+
5+
# Environment files
6+
.env
7+
.env.*
8+
!.env.example
9+
10+
# IDE files
11+
.idea/
12+
.vscode/
13+
*.sublime-project
14+
*.sublime-workspace
15+
16+
# OS generated files
17+
.DS_Store
18+
.DS_Store?
19+
._*
20+
.Spotlight-V100
21+
.Trashes
22+
ehthumbs.db
23+
Thumbs.db
24+
25+
# PHP specific
26+
*.log
27+
*.cache
28+
*.swp
29+
*.swo

examples/dotcms-php/composer.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "dotcms/php-example",
3+
"description": "Example of using dotCMS PHP SDK",
4+
"type": "project",
5+
"repositories": [
6+
{
7+
"type": "path",
8+
"url": "../../"
9+
}
10+
],
11+
"require": {
12+
"php": "^8.2",
13+
"dotcms/php-sdk": "dev-main"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"DotcmsPhpExample\\": "src/"
18+
}
19+
}
20+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
use Dotcms\PhpSdk\Config\Config;
3+
4+
return new Config(
5+
host: 'https://demo.dotcms.com',
6+
apiKey: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhcGlhZmFhNzM1NC04OWFmLTRiYzYtYjcwNC03YjU1ZmM3ZjRlZGIiLCJ4bW9kIjoxNzQ2NDUwMDYzMDAwLCJuYmYiOjE3NDY0NTAwNjMsImlzcyI6IjYxZDZkZDBjNTciLCJleHAiOjE3NDczMTQwNjMsImlhdCI6MTc0NjQ1MDA2MywianRpIjoiNzJkZThhM2EtYzBmNC00ZDg3LWE2ODQtYWI2NjU5NWY4OTBhIn0.0npa2o30ZZeOV_E4h80-bAPHJt7AKBHjphMI5J3elT8',
7+
clientOptions: [
8+
'timeout' => 30,
9+
'verify' => true,
10+
]
11+
);
12+
?>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* Reset box-sizing to border-box for all elements and pseudo-elements */
2+
*, *::before, *::after {
3+
box-sizing: border-box;
4+
}
5+
6+
/* Grid System */
7+
.row {
8+
display: grid;
9+
grid-template-columns: repeat(12, 1fr);
10+
gap: 1rem;
11+
}
12+
13+
.col-start-1 { grid-column-start: 1; }
14+
.col-start-2 { grid-column-start: 2; }
15+
.col-start-3 { grid-column-start: 3; }
16+
.col-start-4 { grid-column-start: 4; }
17+
.col-start-5 { grid-column-start: 5; }
18+
.col-start-6 { grid-column-start: 6; }
19+
.col-start-7 { grid-column-start: 7; }
20+
.col-start-8 { grid-column-start: 8; }
21+
.col-start-9 { grid-column-start: 9; }
22+
.col-start-10 { grid-column-start: 10; }
23+
.col-start-11 { grid-column-start: 11; }
24+
.col-start-12 { grid-column-start: 12; }
25+
26+
.col-end-1 { grid-column-end: 1; }
27+
.col-end-2 { grid-column-end: 2; }
28+
.col-end-3 { grid-column-end: 3; }
29+
.col-end-4 { grid-column-end: 4; }
30+
.col-end-5 { grid-column-end: 5; }
31+
.col-end-6 { grid-column-end: 6; }
32+
.col-end-7 { grid-column-end: 7; }
33+
.col-end-8 { grid-column-end: 8; }
34+
.col-end-9 { grid-column-end: 9; }
35+
.col-end-10 { grid-column-end: 10; }
36+
.col-end-11 { grid-column-end: 11; }
37+
.col-end-12 { grid-column-end: 12; }
38+
.col-end-13 { grid-column-end: 13; }
39+
40+
/* Development helpers - remove in production */
41+
.row {
42+
border: 1px solid blue;
43+
padding: 1rem;
44+
}
45+
46+
.col-start-1[class*="col-end-"] {
47+
border: 2px solid green;
48+
padding: 0.5rem;
49+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* Basic layout styling */
2+
.container {
3+
max-width: 1200px;
4+
margin: 0 auto;
5+
padding: 0 1rem;
6+
}
7+
8+
body {
9+
font-family: Arial, sans-serif;
10+
line-height: 1.5;
11+
margin: 0;
12+
padding: 0;
13+
}
14+
15+
header {
16+
background: #f4f4f4;
17+
padding: 1rem 0;
18+
}
19+
20+
nav ul {
21+
list-style: none;
22+
padding: 0;
23+
margin: 0;
24+
display: flex;
25+
gap: 1rem;
26+
}
27+
28+
nav a {
29+
text-decoration: none;
30+
color: #333;
31+
}
32+
33+
nav a:hover {
34+
color: #666;
35+
}
36+
37+
nav a.active {
38+
font-weight: bold;
39+
color: #000;
40+
}
41+
42+
main {
43+
padding: 2rem 0;
44+
}
45+
46+
footer {
47+
background: #f4f4f4;
48+
padding: 1rem 0;
49+
text-align: center;
50+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
require_once __DIR__ . '/../vendor/autoload.php';
3+
4+
use Dotcms\PhpSdk\DotCMSClient;
5+
6+
// Load configuration
7+
$config = require __DIR__ . '/../config/dotcms.php';
8+
9+
// Initialize client
10+
$client = new DotCMSClient($config);
11+
12+
try {
13+
// Get current path
14+
$path = $_SERVER['REQUEST_URI'] ?? '/';
15+
16+
// Get page and navigation
17+
$pageRequest = $client->createPageRequest($path, 'json');
18+
$pageAsset = $client->getPage($pageRequest);
19+
20+
$navRequest = $client->createNavigationRequest('/', 2);
21+
$nav = $client->getNavigation($navRequest);
22+
23+
include __DIR__ . '/../templates/layout.php';
24+
} catch (Exception $e) {
25+
$statusCode = $e->getCode() ?: 500;
26+
http_response_code($statusCode);
27+
echo "<h1>Error (Status Code: $statusCode)</h1>";
28+
echo "<p>" . htmlspecialchars($e->getMessage()) . "</p>";
29+
}
30+
?>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
$title = $contentlet->title ?? '';
3+
$caption = $contentlet->caption ?? '';
4+
$image = $contentlet->image ?? null;
5+
$link = $contentlet->link ?? '#';
6+
$buttonText = $contentlet->buttonText ?? 'Learn More';
7+
?>
8+
<div style="position: relative; width: 100%; padding: 1rem; background-color: #e5e7eb; height: 24rem;">
9+
<?php if ($image): ?>
10+
<img
11+
src="https://demo.dotcms.com/dA/<?= htmlspecialchars($image->idPath ?? $image) ?>"
12+
style="position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover;"
13+
alt="<?= htmlspecialchars($title) ?>"
14+
/>
15+
<?php endif; ?>
16+
<div style="position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 1rem; text-align: center; color: white;">
17+
<h2 style="margin-bottom: 0.5rem; font-size: 3.75rem; font-weight: bold; text-shadow: 2px 2px 4px rgba(0,0,0,0.5);">
18+
<?= htmlspecialchars($title) ?>
19+
</h2>
20+
<?php if (!empty($caption)): ?>
21+
<p style="margin-bottom: 1rem; font-size: 1.25rem; text-shadow: 2px 2px 4px rgba(0,0,0,0.5);"><?= htmlspecialchars($caption) ?></p>
22+
<?php endif; ?>
23+
<a
24+
href="<?= htmlspecialchars($link) ?>"
25+
style="padding: 1rem; font-size: 1.25rem; background-color: #8b5cf6; color: white; text-decoration: none; border-radius: 0.25rem; transition: background-color 0.3s;"
26+
onmouseover="this.style.backgroundColor='#7c3aed'"
27+
onmouseout="this.style.backgroundColor='#8b5cf6'"
28+
>
29+
<?= htmlspecialchars($buttonText) ?>
30+
</a>
31+
</div>
32+
</div>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
if (!isset($contentlet)) {
3+
echo "<!-- Error: Contentlet data missing -->";
4+
return;
5+
}
6+
7+
$contentTypeVar = $contentlet->contentType ?? 'unknown';
8+
?>
9+
<div class="content-type-not-found">
10+
<p>Template Not Found: <b><?= htmlspecialchars($contentTypeVar) ?></b></p>
11+
</div>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title><?= htmlspecialchars($pageAsset->page->title ?? 'dotCMS Page') ?></title>
5+
<link rel="stylesheet" href="/css/app.css">
6+
<link rel="stylesheet" href="/css/layout.css">
7+
</head>
8+
<body>
9+
<?php if ($pageAsset->layout->header): ?>
10+
<header>
11+
<?php include __DIR__ . '/navigation.php'; ?>
12+
</header>
13+
<?php endif; ?>
14+
15+
<main>
16+
<?php include __DIR__ . '/page.php'; ?>
17+
</main>
18+
19+
<?php if ($pageAsset->layout->footer): ?>
20+
<footer>
21+
<div class="footer-content container">
22+
<p>&copy; <?= date('Y') ?> Your Company Name. All rights reserved.</p>
23+
</div>
24+
</footer>
25+
<?php endif; ?>
26+
</body>
27+
</html>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
// Remove the placeholder content
3+
?>
4+
<nav class="container" role="navigation" aria-label="Main navigation">
5+
<ul>
6+
<?php if (isset($nav)): ?>
7+
<?php
8+
$currentPath = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
9+
foreach ($nav->children as $item):
10+
$isActive = $currentPath === $item->href;
11+
?>
12+
<li>
13+
<a href="<?= htmlspecialchars($item->href ?? '#') ?>"
14+
target="<?= htmlspecialchars($item->target ?? '_self') ?>"
15+
<?= $isActive ? 'class="active"' : '' ?>>
16+
<?= htmlspecialchars($item->title ?? 'Menu Item') ?>
17+
</a>
18+
</li>
19+
<?php endforeach; ?>
20+
<?php else: ?>
21+
<li><a href="/">Home (Fallback)</a></li>
22+
<?php endif; ?>
23+
</ul>
24+
</nav>

0 commit comments

Comments
 (0)