inkphp GitHub 中文

URLs and files

The URL is the file path

URLFileParams
/pages/index.phpnone
/aboutpages/about.phpnone
/blog/hellopages/blog.php['hello']
/admin/userspages/admin/users.phpnone
/robots.txtpages/robots.txt.phpnone

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.

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