命令行 bin/ink
| 命令 | 做什么 |
|---|---|
php bin/ink serve [端口] | 本地开发服务器(默认 8000,APP_ENV=dev) |
php bin/ink routes | 列出每个网址、对应文件、参数个数、请求方法、是否要登录 |
php bin/ink check | 检查语法、页面声明、安全规则,有错误时退出码 1 |
php bin/ink test [--http=网址] | 跑 tests/*.txt 路由表;加 --http 对运行中的服务器实测 |
php bin/ink migrate | 按顺序执行 migrations/*.sql |
check 检查什么(节选)
params()必须是第一条语句,allow()是第二条;- 用了
input()/is_post()却没有allow();参数可选又允许 POST 却只调用一次allow(); - 允许 POST 却没有
csrf_check()(后台是错误,其他是警告,可用csrf_exempt()说明原因); pages/admin…没有require_login();public/里出现别的 PHP 文件;.env.php被提交进 git;storage/不可写;t()漏写了某种语言。
路由表测试
tests/app.txt# tests/app.txt
GET / -> 200 pages/index.php []
GET /about.php -> 301 /about
GET /user/42 -> 200 pages/user.php ["42"]
GET /user/01 -> http:404 # 页面自己决定的状态,只在 --http 时检查
POST /about -> 405
自己写的命令行脚本
bin/import-users#!/usr/bin/env php
<?php
// bin/import-users — php bin/import-users users.csv
require __DIR__ . '/../ink/ink.php'; // 命令行下不发响应头;异常打印到 STDERR,退出码 1
$n = 0;
tx(function () use (&$n): void {
foreach (new SplFileObject($GLOBALS['argv'][1]) as $line) {
if (trim((string) $line) === '') continue;
q('INSERT INTO users (name) VALUES (?)', [trim((string) $line)]);
$n++;
}
});
log_info('users imported', ['rows' => $n]);
用网站同一个用户运行(sudo -u www-data php bin/…);大批量分批提交。