diff --git a/docs_src/src/pages/documentation/en/api_reference/advanced_routing.mdx b/docs_src/src/pages/documentation/en/api_reference/advanced_routing.mdx index 18fcb2cfe..e23afce29 100644 --- a/docs_src/src/pages/documentation/en/api_reference/advanced_routing.mdx +++ b/docs_src/src/pages/documentation/en/api_reference/advanced_routing.mdx @@ -139,6 +139,35 @@ The parameter injection system works in two phases: +### Catch-all (wildcard) routes + + + + A `*name` segment is a **catch-all**: unlike `:name` (which matches a single + path segment), it matches the **rest of the path**, including multiple + `/`-separated levels. The captured remainder is available in + `request.path_params` under that name. + + This is useful for single-page-app fallbacks, API gateways/proxies, and + serving nested file paths. + + + + ```python + @app.get("/files/*path") + async def read_file(request): + # GET /files/img/2024/logo.png -> path == "img/2024/logo.png" + return {"path": request.path_params["path"]} + + # SPA fallback: handle any unmatched sub-path under /app + @app.get("/app/*path") + async def spa(request): + return {"route": request.path_params["path"]} + ``` + + + + ### Route Constraints and Validation diff --git a/docs_src/src/pages/documentation/zh/api_reference/advanced_routing.mdx b/docs_src/src/pages/documentation/zh/api_reference/advanced_routing.mdx index bb35d7659..fcf8801c7 100644 --- a/docs_src/src/pages/documentation/zh/api_reference/advanced_routing.mdx +++ b/docs_src/src/pages/documentation/zh/api_reference/advanced_routing.mdx @@ -139,6 +139,31 @@ Robyn 会自动分析你的函数签名并注入合适的请求组件。这消 +### 通配符(catch-all)路由 + + + + `*name` 段是一个**通配符(catch-all)**:与只匹配单个路径段的 `:name` 不同,它会匹配**路径的其余部分**,包括多个以 `/` 分隔的层级。捕获到的剩余部分可通过 `request.path_params` 以该名称获取。 + + 这对于单页应用(SPA)回退、API 网关 / 代理以及服务嵌套文件路径都很有用。 + + + + ```python + @app.get("/files/*path") + async def read_file(request): + # GET /files/img/2024/logo.png -> path == "img/2024/logo.png" + return {"path": request.path_params["path"]} + + # SPA 回退:处理 /app 下任意未匹配的子路径 + @app.get("/app/*path") + async def spa(request): + return {"route": request.path_params["path"]} + ``` + + + + ### 路由约束与验证