← Blog

A tiny PHP module system

This site has no framework and no Composer. It still has structure, and it took about a dozen small files to get there.

The request path

Every request that isn't a real file is rewritten to index.php:

Request flow

The front controller loads each modules/*/module.php, collects the routes they return, and dispatches. A module definition is just an array:

return [
    'title' => 'Blog',
    'nav'   => 'Blog',
    'url'   => '/blog',
    'routes' => [
        ['GET', '/blog', [Controller::class, 'index']],
        ['GET', '/blog/{slug:[a-z0-9-]+}', [Controller::class, 'show']],
    ],
];

What I like

  1. Adding a module is adding a folder. Nothing else to register.
  2. Everything is plain PHP, so there is little to learn and nothing to upgrade.
  3. Security is centralised. CSRF, headers and escaping live in the core.

What I'd change

The router matches routes in order, so more specific patterns must come first. /blog/feed.xml has to be declared before /blog/{slug}, for example.

Trade-offs

Without a framework there is no ORM, no validation library and no queue. For a personal site that is fine; for anything bigger it would be a poor trade.