主题
12 API 与路由
路由规则
ThinkPHP 多应用 约定路由(config/route.php 中 url_route_must = false):
/{app}/{Controller}/{action}示例:
GET /admin/addon/index?type=addon
POST /admin/marketplace/install
GET /home/content/siteConfig
POST /api/notify/callback?gateway=alipay_1全局仅 route/app.php 定义 / 与 /ping。
开发服务器分流(router.php)
| 请求 | 处理 |
|---|---|
| 真实静态文件 | 直接返回 |
/admin/{ctrl}/{action} | ThinkPHP |
| `/home | api |
/tpl/* | 主题 SSR |
/admin/* 其他 | 后台 SPA index.html |
| 其余 | 前台 SPA index.html |
生产环境由 Nginx 实现相同规则,见 13-部署运维。
响应格式
统一 JSON(ApiResponse trait):
json
{
"status": 200,
"msg": "ok",
"data": { }
}错误:status 4xx/5xx,msg 为可读信息。
鉴权
| 应用 | 方式 |
|---|---|
| admin | Header Authorization: Bearer {token} |
| home 会员接口 | 同上(UserAuth 中间件) |
| home 公开接口 | 无 token(siteConfig、banners 等) |
后台权限模块
AdminPermissionService::MODULES 定义模块键,控制器映射见 CONTROLLER_MAP。
插件相关:
Addon、Marketplace→ 需plugin权限
常用公开 API
| 接口 | 说明 |
|---|---|
| GET /home/content/siteConfig | 站点+主题 bundle |
| GET /home/content/banners | 轮播 |
| GET /ping | 探活 |
支付回调
POST/GET /api/notify/callback?gateway={网关name}
GET /api/notify/sync?gateway=&invoice=由 PaymentService::handleNotify() 验签并标记账单。
下一章:13-部署运维