普贤教程 › 包参考
webroute 包
文件即路由命名规则(M43)
纯函数:文件名(相对应用根)→ 路由规则 dict。
| 项 | 值 |
|---|---|
| 包名 | webroute |
| 版本 | 0.1.0 |
| 入口 | webroute.px |
| 分类 | Web 与 HTTP |
| 文件数 / 行数 | 1 / 113 |
安装与引入
bash
pxpkg init # 首次 pxpkg add webroute@0.1.0 pxpkg install
px
import webroute所属分类教程:Web 与 HTTP
公开接口
| 函数 | 参数 |
|---|---|
wr_parse_file | relpath |
其余 3 个 def(内部与辅助,可直接调用但不属稳定接口)
| 函数 | 参数 |
|---|---|
wr_methods | |
wr_is_special | name |
wr_seg_to_pattern | seg |
包内说明(源码头注释)
text
============================================================ 普贤标准库:std.webroute —— 文件即路由命名规则(M43) ------------------------------------------------------------ 纯函数:文件名(相对应用根)→ 路由规则 dict。 用法:wr_parse_file("get_healthz.px") → {"method":"GET","pattern":"/healthz","fnname":"get_healthz"} wr_parse_file("main.px") → null(非路由文件) 被 tools/routegen.px(路由生成器)与 capability.px(能力自检)复用。 双模式共享:编译(pxc build)+ 解释(pxi run)输出一致。 规则摘要: - 方法前缀:文件名首段(第一个 _ 前)∈ 方法表 → 该值大写;否则 GET - 路径段:目录段与文件名段统一按 `_` 拆分(_ 即路径分隔);#name → :name;* → * - index.px / get.px → GET / - fnname = relpath 去 .px、/ → _、# 移除(全局唯一,重复由 routegen 报错) - 特殊文件(main/middleware/generated_routes)与 _ 开头(私有)、static/ 下 → null ============================================================ ---- 方法表(首段 ∈ 此表 → 方法前缀)----