Routing
The router checks with reflection that a path names a public action before anything runs.
A router, a PDO data layer, and plain PHP views. Enough structure to build with. Little enough to read in full.
01 / In practice
These posts come from the demo’s SQLite database. Open the source below to see the controller, model, and view that put them here.
GET /home/indexThe router checks with reflection that a path names a public action before anything runs.
Every value is bound with a PDO type taken from its PHP type, never joined into SQL.
Templates are plain PHP, escaped with htmlspecialchars and rendered inside one layout.
<?php
declare(strict_types=1);
namespace App\Controllers;
use App\Core\Controller;
use App\Models\Post;
use RuntimeException;
final class Home extends Controller
{
public function index(): string
{
return $this->render('home/index', [
'title' => 'GalaxyMVC',
'posts' => (new Post($this->db))->all(),
'sources' => $this->source(),
]);
}
public function about(): string
{
return $this->render('home/about', ['title' => 'Guide']);
}
/** @return array<string, string> */
private function source(): array
{
// Only these public demo files can be displayed; no request value becomes a file path.
$sources = [];
foreach (['Controllers/Home.php', 'Models/Post.php', 'views/home/posts.php'] as $path) {
$contents = file_get_contents(dirname(__DIR__) . '/' . $path);
if ($contents === false) {
throw new RuntimeException('Demo source is unavailable.');
}
$sources['app/' . $path] = $contents;
}
return $sources;
}
}
Open on GitHub
<?php
declare(strict_types=1);
namespace App\Models;
use App\Core\Database;
final class Post
{
public function __construct(private readonly Database $db)
{
}
/**
* @return list<array<string, mixed>>
*/
public function all(): array
{
return $this->db->select('SELECT id, title, content FROM posts ORDER BY id');
}
}
Open on GitHub
<?php
declare(strict_types=1);
/** @var list<array<string, mixed>> $posts */
?>
<div class="query-results" aria-label="Records rendered from the database">
<?php foreach ($posts as $post) : ?>
<article class="result-row">
<span class="row-number">#<?= htmlspecialchars((string) $post['id']) ?></span>
<h3><?= htmlspecialchars((string) $post['title']) ?></h3>
<p><?= htmlspecialchars((string) $post['content']) ?></p>
</article>
<?php endforeach; ?>
<?php if ($posts === []) : ?>
<p class="empty-state">No posts in the database. Run <code>composer setup</code> in a fresh local checkout to load the sample records.</p>
<?php endif; ?>
</div>
Open on GitHub
02 / Make it yours
Install it. Follow a request.
Build from there.