Skip to content

Instantly share code, notes, and snippets.

@sroehrl
Created November 6, 2021 17:50
Show Gist options
  • Save sroehrl/d70261baf7273e572d8ce146476aebba to your computer and use it in GitHub Desktop.
Save sroehrl/d70261baf7273e572d8ce146476aebba to your computer and use it in GitHub Desktop.
PHP: Most primitive blog - using less than a total of 75 lines of code (uses blua.blue as headless CMS)
<?php
$current = $_GET['entry'] ?? null;
$all = [];
$menu = '';
foreach (glob('archieve/*.json') as $i => $entry) {
$all[] = json_decode(file_get_contents($entry), true);
if (!$current) {
$current = $all[0]['slug'];
}
// menu
$src = '?entry=' . $all[$i]['slug'];
$title = $all[$i]['name'];
$menu .= "<li class='nav-item'><a class='nav-link' href='$src'>$title</a></li>";
}
$thisArticle = array_filter($all, fn($item) => $item['slug'] === $current)[0];
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<title>Blog</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<div class="container">
<a class="navbar-brand text-uppercase " href="#">Most primitive blog</a>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-8">
<h1><?= $thisArticle['name'] ?></h1>
<p><?= $thisArticle['teaser'] ?></p>
<hr>
<?php foreach ($thisArticle['article_content'] as $content) {
echo $content['html'];
} ?>
</div>
<div class="col">
<ul class="nav flex-column">
<?= $menu ?>
</ul>
</div>
</div>
</div>
</body>
</html>
<?php
$bluaBlueInstance = 'https://blua.blue';
header("Access-Control-Allow-Origin: $bluaBlueInstance", false);
header("Content-Type: application/json");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
}
$data = file_get_contents('php://input');
if ($data) {
$converted = json_decode($data, true);
$fileName = 'archieve/' . $converted['payload']['slug'] . '.json';
if ($converted['event'] === 'deleted') {
unlink($fileName);
} else {
file_put_contents($fileName, json_encode($converted['payload']));
}
}
echo json_encode(['received' => true]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment