Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,35 @@ The parameter injection system works in two phases:
</Col>
</Row>

### Catch-all (wildcard) routes

<Row>
<Col>
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.
</Col>
<Col sticky>
<CodeGroup title="Catch-all routes">
```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"]}
```
</CodeGroup>
</Col>
</Row>

### Route Constraints and Validation

<Row>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,31 @@ Robyn 会自动分析你的函数签名并注入合适的请求组件。这消
</Col>
</Row>

### 通配符(catch-all)路由

<Row>
<Col>
`*name` 段是一个**通配符(catch-all)**:与只匹配单个路径段的 `:name` 不同,它会匹配**路径的其余部分**,包括多个以 `/` 分隔的层级。捕获到的剩余部分可通过 `request.path_params` 以该名称获取。

这对于单页应用(SPA)回退、API 网关 / 代理以及服务嵌套文件路径都很有用。
</Col>
<Col sticky>
<CodeGroup title="通配符路由">
```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"]}
```
</CodeGroup>
</Col>
</Row>

### 路由约束与验证

<Row>
Expand Down
Loading