inkphp GitHub 中文

Function reference

All 45 public functions. Full descriptions and examples are in the project's AGENTS.md. Functions starting with ink_ are kernel internals; pages don't call them.

Routing

params(int $min = 0, ?int $max = null): arraydeclare and get URL params; wrong count → 404
as_id(?string $v): ?intcanonical positive id; null → null; anything else → 404
allow(string ...$methods): voidallowed methods; others → 405; call again to narrow

Request

input(?string $key = null, mixed $default = null): mixedform fields or JSON body (raw, untrimmed)
is_post(): boolPOST request?
is_htmx(): boolhtmx request?
wants_json(): boolAccept wants JSON, or the path is under /api
host(): stringlowercased Host; invalid → 400
is_https(): boolHTTPS?
client_ip(): stringvisitor IP (REMOTE_ADDR)
ip_key(): stringIP for rate limiting (IPv6 per /64)

Response

json(mixed $data, int $code = 200): neversend JSON and stop
redirect(string $to, int $code = 302): neverlocal redirect; external targets are refused
abort(int $code, string $message = ''): nevererror page (HTML or JSON)
url(string $path = '/'): stringabsolute URL (config base_url or the request Host)
cache_public(int $seconds): voidlet browsers and CDNs cache
store_upload(string $field, array $types = [...], int $maxBytes = 5_000_000): ?stringsave an upload, return its URL
upload_error(): ?stringwhy the last upload failed
asset(string $path): string/assets/… with a version

Views

e(mixed $v): stringHTML escape
layout(?string $name, array $vars = []): voiduse a layout; null cancels
t(array|string $s, array $vars = []): stringinline translation
locale(mixed $set = null, bool $remember = true): stringcurrent language; locale('zh', false) for this request only

Data

db(): PDOthe PDO connection (lazy)
q(string $sql, array $params = []): PDOStatementrun SQL
one(string $sql, array $params = []): ?arrayone row or null
all(string $sql, array $params = []): arrayall rows
val(string $sql, array $params = []): mixedfirst column of the first row
tx(callable $fn): mixedtransaction
validate(array $data, array $rules): array[$clean, $errors]

Session & security

session(): voidstart the session
flash(string $key, ?string $msg = null): ?stringone-time message
csrf_token(): stringCSRF token
csrf_field(): stringhidden form field
csrf_check(): voidcheck the token, else 403
csrf_exempt(string $reason): voiddeclare a page intentionally without CSRF
login(int|string $id): voidlog in (rotates the session id)
logout(): voidlog out
user(): int|string|nullcurrent user id
require_login(string $loginPath = '/login', bool $hidden = false): int|stringrequire login; hidden → 404
rate_limit(string $key, int $max, int $windowSeconds): voidrate limit, else 429

Config & logs

config(string $key, mixed $default = null): mixedread config.php (dot paths)
env(string $key, mixed $default = null): mixedread .env.php or the environment
log_info(string $msg, array $ctx = []): voidwrite a JSON log line
log_error(string $msg, array $ctx = []): voiderror log line