Capstone: pastebin app
The capstone puts the pieces of the course together in one small PHP application: a route map, a file store and a page that reads both. Every program below was run here, the front controller through php -S.
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.
<?php $routes = [ "new" => "showForm", "save" => "savePaste", "view" => "showPaste", ]; echo count($routes), "\n";
php routes.php
3
The map is the whole specification of the app in three lines, and each handler name says what the request does.
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.
<?php $folder = __DIR__ . "/data"; if (!is_dir($folder)) { mkdir($folder, 0775); } function pastePath(string $id): string { return __DIR__ . "/data/" . $id . ".txt"; } function savePasteText(string $id, string $text): void { file_put_contents(pastePath($id), $text); } savePasteText("abc123", "hello paste"); echo file_get_contents(pastePath("abc123")), PHP_EOL;
php store.php
hello paste
The same id built both the write and the read, so the text came back from the file the app created. A real app checks that the id is letters and digits before it reaches pastePath().
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.
The file index.php in the folder you serve with php -S 127.0.0.1:8000 index.php:
<?php $path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH); if ($path === "/new") { echo "form\n"; } if ($path === "/view") { echo "paste\n"; }
curl -s "http://127.0.0.1:8000/new"
form
The path decided the handler, and the same file answers paste for /view and an empty body for a path it does not know; both were checked with the same server. The router argument of php -S is what sends every path to this file.
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.
<?php $routes = [ "new" => "showForm", "save" => "savePaste", "view" => "showPaste", ]; 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");
php tests.php
OK: route new exists
One check proves one promise, and the helper from the testing lesson is the whole runner. Add a check for the store and the app has a hand written test suite of two lines. The rest of the course sits in PHP course, chapter by chapter, if you want to revisit a part.
Exercises
- Add a delete route and its handler name to the map.
- Give pastePath() a check that refuses an id with anything but letters and digits.
- Add a /save branch that writes a paste and answers its id.
- Write two checks, one for the route map and one for the store, and run them as one script.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does the route map of the capstone connect?
Answer
A path to a handler name
The front controller reads the path, looks it up, and calls the handler the map names.
-
Where does the file store keep one paste?
Answer
In one text file named by the id
The id builds the file name inside the data folder, and the text is the whole file.
-
What does the front controller read to choose a handler?
Answer
The request path
parse_url()ofREQUEST_URIgives the path, and the router sends every path to this one file. -
Why does the id need a strict check before it becomes a path?
Answer
A value with slashes or dots can point outside the data folder
An id such as
../../etc/passwdbecomes a path to another file, which is the traversal bug. -
What does this program print?
<?php $routes = [ "new" => "showForm", "save" => "savePaste", "view" => "showPaste", ]; echo count($routes);Answer
3
The map holds three entries, one per route of the spec.
-
What does this program print, run in an empty folder?
<?php $folder = __DIR__ . "/data"; if (!is_dir($folder)) { mkdir($folder, 0775); } function pastePath(string $id): string { return __DIR__ . "/data/" . $id . ".txt"; } function savePasteText(string $id, string $text): void { file_put_contents(pastePath($id), $text); } savePasteText("abc123", "hello"); echo file_get_contents(pastePath("abc123"));Answer
hello
The write creates the file, and the read with the same id returns its contents.
-
What does this program print?
<?php $path = "/view"; if ($path === "/new") { echo "form\n"; } if ($path === "/view") { echo "paste\n"; }Answer
paste
Only the second test matches the path, so only its line prints.
-
What does this program print?
<?php $routes = ["new" => "showForm"]; function check(bool $cond, string $label): void { if (!$cond) { echo "FAIL: " . $label . "\n"; exit(1); } echo "OK: " . $label . "\n"; } check(isset($routes["view"]), "route view exists");Answer
FAIL: route view exists
The map has no view entry, so the condition is false and the helper reports the failure.
-
Why does the front controller answer an empty body for an unknown path?
Answer
Because no branch matched, and the file has no fallback
The two if statements cover two paths, and a real app needs a third branch that answers 404.
-
What is the value of a hand written check before the deploy?
Answer
It proves the store and the routes with one command
One script prints a line per promise, so a deploy starts from a green run instead of a hope.
The examples call count(), file_get_contents() and parse_url(). The programs on this page were run with PHP 8.5.13 (php -v); the front controller was served with php -S 127.0.0.1:8000 index.php.
Article author: Arthur Isaev