Capstone: pastebin app

The spec

The capstone builds a pastebin: a form accepts text, the app stores it under a short id, and a view page shows it back. Three routes cover everything: new for the form, save for storing, view for reading.

$routes = [ "new" => "showForm", "save" => "savePaste", "view" => "showPaste", ]; echo count($routes) . "\n";

File store

Each paste is one text file named by its id inside a data folder, which keeps the project database free and easy to inspect. Writing uses one put call, reading uses one get call, and ids get strict checks before touching disk.

function pastePath(string $id): string { return __DIR__ . "/data/" . $id . ".txt"; } function savePasteText(string $id, string $text): void { file_put_contents(pastePath($id), $text); }

Front controller

Every request enters one index file that reads the path and calls the matching handler. Clean links like view plus id work through rewrite rules that forward all paths to that single entry point.

$path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH); if ($path === "/new") { echo "form\n"; } if ($path === "/view") { echo "paste\n"; }

Hand tests

Finish with plain script checks: save a paste to a temp folder, read it back, compare the bytes. Green checks on store and routes mean the app is ready to deploy with the checklist from the deploy lesson.

function check(bool $cond, string $label): void { if (!$cond) { echo "FAIL: " . $label . "\n"; exit(1); } echo "OK: " . $label . "\n"; } check(isset($routes["new"]), "route new exists");

Next: PHP course

Article author: Arthur Isaev