URLs and files
The URL is the file path
| URL | File | Params |
|---|---|---|
/ | pages/index.php | none |
/about | pages/about.php | none |
/blog/hello | pages/blog.php | ['hello'] |
/admin/users | pages/admin/users.php | none |
/robots.txt | pages/robots.txt.php | none |
The longest path wins: /a/b/c tries pages/a/b/c.php, then pages/a/b.php, then pages/a.php; the first file that exists handles the request and the remaining segments are its params. If none exists, pages/index.php gets them all.
- File names are lowercase:
[a-z0-9._-]. Files and directories starting with_or.are never reachable. x.phpand a directoryx/can coexist:/x→x.php,/x/y→x/y.php.- Don't create
pages/x/index.php(it is never routed); usepages/x.php.
Canonical URLs (one 301)
/about.php, /about/, //about and /index all 301 to the canonical form, so a page has exactly one URL. A URL containing .. gets 400.
params(): how many URL params a page takes
params(); // no params (the usual case)
[$slug] = params(1); // exactly 1
[$id, $tab] = params(1, 2); // 1 to 2, padded with null
Wrong count → 404. Params are strings and can contain anything: validate them. Non-ASCII params are decoded once, and '0' is a valid param. params() must be the first statement of a page.
as_id(): record ids
[$raw] = params(1);
$row = one('SELECT * FROM notes WHERE id = ?', [as_id($raw)]) ?? abort(404);
Only canonical positive integers pass: /notes/01, /notes/0 and /notes/abc are 404, so one record has one URL.
allow(): which methods a page accepts
allow('GET', 'POST'); // 2nd statement; any other method → 405 + Allow header
A page without allow() accepts GET/HEAD only. When params are optional and the page accepts POST, call allow() again to say what the URL with a param accepts:
[$raw] = params(0, 1); // /notes or /notes/<id>
allow('GET', 'POST'); // /notes: list + create
$id = as_id($raw);
if ($id !== null) allow('GET'); // /notes/<id>: read only, POST → 405