KScript 文档
ZH

KScript.Http

路由、控制器和中间件:由 kscript serve 提供服务的 <KScript.Routes> 文件。

在脚本顶部导入:

<import package="KScript.Http" />
import KScript.Http

根元素为 <KScript.Route> 的文件会为你导入它。

.kscript 文件只在导入语句中指定包名:写 Route { },不要写 KScript.Route { }。

KScript.Http 提供 Web 路由,因此其示例无法在文档中运行。将其复制到 .ks.xml 文件中,并用 kscript 运行。

元素语句

#<Any>Any

用于任意请求方法访问某路径的路由。由控制器(to)、视图、重定向或其中的语句(<return value="..." />)处理。

包含语句。在 { } 中包含语句。仅限脚本。放在 <KScript.Routes>Routes 或 <Group>Group 内。

属性选项值说明
path 必需a string

The path, with parameters: /people/. {name?} is optional and {name*} takes the rest of the path.

to函数:@name控制器:一个 <fn>(@show)或已导入脚本的一个函数(@people.show);在 XML 中为 to="people.show"。其参数按名称填充:路径参数、request、json、query、form、input、headers。控制器:一个 fn(@show)或已导入脚本的一个函数(@people.show);在 XML 中为 to="people.show"。其参数按名称填充:路径参数、request、json、query、form、input、headers。
viewa string改为从 views 文件夹返回此视图,并将路径参数作为变量。
redirecta string改为重定向到此地址。
status表达式重定向的状态码,默认为 302。
namea string路由的名称,用于 @route('name', ...)。
label_keya string路由显示标签的翻译键。
data表达式由已声明的记录类型构成的可选路由元数据。
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_response应用于此路由的中间件,以逗号分隔。
cana string控制器运行前模型策略必须允许的权限:can: update。系统会就控制器的类型化路径参数所指的记录进行询问,即 fn update(post: Post)。返回否时响应 403;被该权限拒绝的访客会被引导登录(API 则返回 401)。
wherea string参数规则:where: { id: int, slug: slug }(XML 中为 where="id: int, slug: slug")。规则可以是 number、int、alpha、alnum、slug、uuid 或正则表达式。
etagtrue 或 false设为 true 时,GET 响应会带上 ETag(正文的哈希值);当客户端已有该版本时,对 If-None-Match 返回 304 Not Modified。
cachea stringGET 响应的 Cache-Control 头:public, max-age=300,或仅为秒数(300 即公开缓存 5 分钟)、private、no-store……
示例
<KScript.Routes>
  <Any path="/people/{id}" to="people.show" name="people.show" where="id: int" />
</KScript.Routes>
import KScript.Http

Routes {
  Any(path: "/people/{id}", to: @people.show, name: 'people.show', where: 'id: int')
}

#<Delete>Delete

用于接收 DELETE 请求的路由。由控制器(to)、视图、重定向或其中的语句(<return value="..." />)处理。

包含语句。在 { } 中包含语句。仅限脚本。放在 <KScript.Routes>Routes 或 <Group>Group 内。

属性选项值说明
path 必需a string

The path, with parameters: /people/. {name?} is optional and {name*} takes the rest of the path.

to函数:@name控制器:一个 <fn>(@show)或已导入脚本的一个函数(@people.show);在 XML 中为 to="people.show"。其参数按名称填充:路径参数、request、json、query、form、input、headers。控制器:一个 fn(@show)或已导入脚本的一个函数(@people.show);在 XML 中为 to="people.show"。其参数按名称填充:路径参数、request、json、query、form、input、headers。
viewa string改为从 views 文件夹返回此视图,并将路径参数作为变量。
redirecta string改为重定向到此地址。
status表达式重定向的状态码,默认为 302。
namea string路由的名称,用于 @route('name', ...)。
label_keya string路由显示标签的翻译键。
data表达式由已声明的记录类型构成的可选路由元数据。
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_response应用于此路由的中间件,以逗号分隔。
cana string控制器运行前模型策略必须允许的权限:can: update。系统会就控制器的类型化路径参数所指的记录进行询问,即 fn update(post: Post)。返回否时响应 403;被该权限拒绝的访客会被引导登录(API 则返回 401)。
wherea string参数规则:where: { id: int, slug: slug }(XML 中为 where="id: int, slug: slug")。规则可以是 number、int、alpha、alnum、slug、uuid 或正则表达式。
etagtrue 或 false设为 true 时,GET 响应会带上 ETag(正文的哈希值);当客户端已有该版本时,对 If-None-Match 返回 304 Not Modified。
cachea stringGET 响应的 Cache-Control 头:public, max-age=300,或仅为秒数(300 即公开缓存 5 分钟)、private、no-store……
示例
<KScript.Routes>
  <Delete path="/people/{id}" to="people.destroy" middleware="auth" />
</KScript.Routes>
import KScript.Http

Routes {
  Delete(path: "/people/{id}", to: @people.destroy, middleware: auth)
}

#<Get>Get

用于接收 GET 请求的路由。由控制器(to)、视图、重定向或其中的语句(<return value="..." />)处理。

包含语句。在 { } 中包含语句。仅限脚本。放在 <KScript.Routes>Routes 或 <Group>Group 内。

属性选项值说明
path 必需a string

The path, with parameters: /people/. {name?} is optional and {name*} takes the rest of the path.

to函数:@name控制器:一个 <fn>(@show)或已导入脚本的一个函数(@people.show);在 XML 中为 to="people.show"。其参数按名称填充:路径参数、request、json、query、form、input、headers。控制器:一个 fn(@show)或已导入脚本的一个函数(@people.show);在 XML 中为 to="people.show"。其参数按名称填充:路径参数、request、json、query、form、input、headers。
viewa string改为从 views 文件夹返回此视图,并将路径参数作为变量。
redirecta string改为重定向到此地址。
status表达式重定向的状态码,默认为 302。
namea string路由的名称,用于 @route('name', ...)。
label_keya string路由显示标签的翻译键。
data表达式由已声明的记录类型构成的可选路由元数据。
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_response应用于此路由的中间件,以逗号分隔。
cana string控制器运行前模型策略必须允许的权限:can: update。系统会就控制器的类型化路径参数所指的记录进行询问,即 fn update(post: Post)。返回否时响应 403;被该权限拒绝的访客会被引导登录(API 则返回 401)。
wherea string参数规则:where: { id: int, slug: slug }(XML 中为 where="id: int, slug: slug")。规则可以是 number、int、alpha、alnum、slug、uuid 或正则表达式。
etagtrue 或 false设为 true 时,GET 响应会带上 ETag(正文的哈希值);当客户端已有该版本时,对 If-None-Match 返回 304 Not Modified。
cachea stringGET 响应的 Cache-Control 头:public, max-age=300,或仅为秒数(300 即公开缓存 5 分钟)、private、no-store……
示例
<KScript.Routes>
  <Get path="/people/{id}" to="people.show" name="people.show" where="id: int" />
</KScript.Routes>
import KScript.Http

Routes {
  Get(path: "/people/{id}", to: @people.show, name: 'people.show', where: 'id: int')
}

#<Group>Group

共享路径前缀、中间件或名称前缀的路由。

包含语句。在 { } 中包含语句。仅限脚本。放在 <KScript.Routes>Routes 或 <Group>Group 内。

属性选项值说明
prefixa string置于其中所有路由之前的路径,例如 /admin。
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_response应用于其中所有路由的中间件,以逗号分隔。
namea string置于其中所有路由名称之前的前缀,例如 admin。
示例
<KScript.Routes>
  <Group prefix="/admin" middleware="auth" name="admin.">
    <Get path="/" to="admin.dashboard" name="home" />
  </Group>
</KScript.Routes>
import KScript.Http

Routes {
  Group(prefix: '/admin', middleware: auth, name: 'admin.') {
    Get(path: '/', to: @admin.dashboard, name: 'home')
  }
}

#<Middleware>Middleware

应用于此文件或分组中后续路由的中间件。

自闭合。独立的语句。仅限脚本。放在 <KScript.Routes>Routes 或 <Group>Group 内。

属性选项值说明
use 必填log | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_response在后续路由之前运行的内置函数或自定义函数。
示例
<KScript.Routes>
  <Middleware use="log, cors" />
</KScript.Routes>
import KScript.Http

Routes {
  Middleware(use: [log, cors])
}

#<ModelUpdate>ModelUpdate

允许命名的 HTML 模型表单编辑已登录用户所拥有记录的指定字段。

自闭合。独立的语句。仅限脚本。放在 <KScript.Routes>Routes 或 <Group>Group 内。

属性选项值说明
name 必填a string供 <form model-update> 使用的绑定名称。
model 必填a string项目 models 文件夹中的模型。
owner 必填a string保存已认证用户 ID 的列。
fields 必填名称列表:[a, b]可写的模型字段,例如 [title, body]。
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_response此绑定额外的授权或其他中间件。
示例
<KScript.Routes><ModelUpdate name="edit_note" model="Note" owner="user_id" fields="title, body" /></KScript.Routes>
import KScript.Http

Routes {
  ModelUpdate(name: edit_note, model: Note, owner: user_id, fields: [title, body])
}

#<Options>Options

用于接收 OPTIONS 请求的路由。由控制器(to)、视图、重定向或其中的语句(<return value="..." />)处理。

包含语句。在 { } 中包含语句。仅限脚本。放在 <KScript.Routes>Routes 或 <Group>Group 内。

属性选项值说明
path 必需a string

The path, with parameters: /people/. {name?} is optional and {name*} takes the rest of the path.

to函数:@name控制器:一个 <fn>(@show)或已导入脚本的一个函数(@people.show);在 XML 中为 to="people.show"。其参数按名称填充:路径参数、request、json、query、form、input、headers。控制器:一个 fn(@show)或已导入脚本的一个函数(@people.show);在 XML 中为 to="people.show"。其参数按名称填充:路径参数、request、json、query、form、input、headers。
viewa string改为从 views 文件夹返回此视图,并将路径参数作为变量。
redirecta string改为重定向到此地址。
status表达式重定向的状态码,默认为 302。
namea string路由的名称,用于 @route('name', ...)。
label_keya string路由显示标签的翻译键。
data表达式由已声明的记录类型构成的可选路由元数据。
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_response应用于此路由的中间件,以逗号分隔。
cana string控制器运行前模型策略必须允许的权限:can: update。系统会就控制器的类型化路径参数所指的记录进行询问,即 fn update(post: Post)。返回否时响应 403;被该权限拒绝的访客会被引导登录(API 则返回 401)。
wherea string参数规则:where: { id: int, slug: slug }(XML 中为 where="id: int, slug: slug")。规则可以是 number、int、alpha、alnum、slug、uuid 或正则表达式。
etagtrue 或 false设为 true 时,GET 响应会带上 ETag(正文的哈希值);当客户端已有该版本时,对 If-None-Match 返回 304 Not Modified。
cachea stringGET 响应的 Cache-Control 头:public, max-age=300,或仅为秒数(300 即公开缓存 5 分钟)、private、no-store……
示例
<KScript.Routes>
  <Options path="/people/{id}" to="people.update" middleware="auth" />
</KScript.Routes>
import KScript.Http

Routes {
  Options(path: "/people/{id}", to: @people.update, middleware: auth)
}

#<Patch>Patch

用于接收 PATCH 请求的路由。由控制器(to)、视图、重定向或其中的语句(<return value="..." />)处理。

包含语句。在 { } 中包含语句。仅限脚本。放在 <KScript.Routes>Routes 或 <Group>Group 内。

属性选项值说明
path 必需a string

The path, with parameters: /people/. {name?} is optional and {name*} takes the rest of the path.

to函数:@name控制器:一个 <fn>(@show)或已导入脚本的一个函数(@people.show);在 XML 中为 to="people.show"。其参数按名称填充:路径参数、request、json、query、form、input、headers。控制器:一个 fn(@show)或已导入脚本的一个函数(@people.show);在 XML 中为 to="people.show"。其参数按名称填充:路径参数、request、json、query、form、input、headers。
viewa string改为从 views 文件夹返回此视图,并将路径参数作为变量。
redirecta string改为重定向到此地址。
status表达式重定向的状态码,默认为 302。
namea string路由的名称,用于 @route('name', ...)。
label_keya string路由显示标签的翻译键。
data表达式由已声明的记录类型构成的可选路由元数据。
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_response应用于此路由的中间件,以逗号分隔。
cana string控制器运行前模型策略必须允许的权限:can: update。系统会就控制器的类型化路径参数所指的记录进行询问,即 fn update(post: Post)。返回否时响应 403;被该权限拒绝的访客会被引导登录(API 则返回 401)。
wherea string参数规则:where: { id: int, slug: slug }(XML 中为 where="id: int, slug: slug")。规则可以是 number、int、alpha、alnum、slug、uuid 或正则表达式。
etagtrue 或 false设为 true 时,GET 响应会带上 ETag(正文的哈希值);当客户端已有该版本时,对 If-None-Match 返回 304 Not Modified。
cachea stringGET 响应的 Cache-Control 头:public, max-age=300,或仅为秒数(300 即公开缓存 5 分钟)、private、no-store……
示例
<KScript.Routes>
  <Patch path="/people/{id}" to="people.update" middleware="auth" />
</KScript.Routes>
import KScript.Http

Routes {
  Patch(path: "/people/{id}", to: @people.update, middleware: auth)
}

#<Post>Post

用于接收 POST 请求的路由。由控制器(to)、视图、重定向或其中的语句(<return value="..." />)处理。

包含语句。在 { } 中包含语句。仅限脚本。放在 <KScript.Routes>Routes 或 <Group>Group 内。

属性选项值说明
path 必需a string

The path, with parameters: /people/. {name?} is optional and {name*} takes the rest of the path.

to函数:@name控制器:一个 <fn>(@show)或已导入脚本的一个函数(@people.show);在 XML 中为 to="people.show"。其参数按名称填充:路径参数、request、json、query、form、input、headers。控制器:一个 fn(@show)或已导入脚本的一个函数(@people.show);在 XML 中为 to="people.show"。其参数按名称填充:路径参数、request、json、query、form、input、headers。
viewa string改为从 views 文件夹返回此视图,并将路径参数作为变量。
redirecta string改为重定向到此地址。
status表达式重定向的状态码,默认为 302。
namea string路由的名称,用于 @route('name', ...)。
label_keya string路由显示标签的翻译键。
data表达式由已声明的记录类型构成的可选路由元数据。
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_response应用于此路由的中间件,以逗号分隔。
cana string控制器运行前模型策略必须允许的权限:can: update。系统会就控制器的类型化路径参数所指的记录进行询问,即 fn update(post: Post)。返回否时响应 403;被该权限拒绝的访客会被引导登录(API 则返回 401)。
wherea string参数规则:where: { id: int, slug: slug }(XML 中为 where="id: int, slug: slug")。规则可以是 number、int、alpha、alnum、slug、uuid 或正则表达式。
etagtrue 或 false设为 true 时,GET 响应会带上 ETag(正文的哈希值);当客户端已有该版本时,对 If-None-Match 返回 304 Not Modified。
cachea stringGET 响应的 Cache-Control 头:public, max-age=300,或仅为秒数(300 即公开缓存 5 分钟)、private、no-store……
示例
<KScript.Routes>
  <Post path="/people/{id}" to="people.store" middleware="auth" />
</KScript.Routes>
import KScript.Http

Routes {
  Post(path: "/people/{id}", to: @people.store, middleware: auth)
}

#<Put>Put

用于接收 PUT 请求的路由。由控制器(to)、视图、重定向或其中的语句(<return value="..." />)处理。

包含语句。在 { } 中包含语句。仅限脚本。放在 <KScript.Routes>Routes 或 <Group>Group 内。

属性选项值说明
path 必需a string

The path, with parameters: /people/. {name?} is optional and {name*} takes the rest of the path.

to函数:@name控制器:一个 <fn>(@show)或已导入脚本的一个函数(@people.show);在 XML 中为 to="people.show"。其参数按名称填充:路径参数、request、json、query、form、input、headers。控制器:一个 fn(@show)或已导入脚本的一个函数(@people.show);在 XML 中为 to="people.show"。其参数按名称填充:路径参数、request、json、query、form、input、headers。
viewa string改为从 views 文件夹返回此视图,并将路径参数作为变量。
redirecta string改为重定向到此地址。
status表达式重定向的状态码,默认为 302。
namea string路由的名称,用于 @route('name', ...)。
label_keya string路由显示标签的翻译键。
data表达式由已声明的记录类型构成的可选路由元数据。
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_response应用于此路由的中间件,以逗号分隔。
cana string控制器运行前模型策略必须允许的权限:can: update。系统会就控制器的类型化路径参数所指的记录进行询问,即 fn update(post: Post)。返回否时响应 403;被该权限拒绝的访客会被引导登录(API 则返回 401)。
wherea string参数规则:where: { id: int, slug: slug }(XML 中为 where="id: int, slug: slug")。规则可以是 number、int、alpha、alnum、slug、uuid 或正则表达式。
etagtrue 或 false设为 true 时,GET 响应会带上 ETag(正文的哈希值);当客户端已有该版本时,对 If-None-Match 返回 304 Not Modified。
cachea stringGET 响应的 Cache-Control 头:public, max-age=300,或仅为秒数(300 即公开缓存 5 分钟)、private、no-store……
示例
<KScript.Routes>
  <Put path="/people/{id}" to="people.update" middleware="auth" />
</KScript.Routes>
import KScript.Http

Routes {
  Put(path: "/people/{id}", to: @people.update, middleware: auth)
}

#<Resource>Resource

The usual routes of a JSON resource, answered by the functions a controller script exports: index (GET path), store (POST path), show (GET path/), update (PUT and PATCH path/) and destroy (DELETE path/). Functions the controller doesn't have get no route.

自闭合。独立的语句。仅限脚本。放在 <KScript.Routes>Routes 或 <Group>Group 内。

属性选项值说明
path 必需a string资源路径,例如 /people。
to 必填a string控制器:脚本被导入时所用的名称(<import path="..." as="people" />)。
onlyindex | store | show | update | destroy仅限以下操作:[index, show]。
exceptindex | store | show | update | destroy除以下操作外的所有操作:[destroy]。
namea string路由名称前缀(people 会生成 people.index、people.show……)。默认为控制器的名称。
parama string路径参数的名称,默认为 id。
modela string该资源所涉及的模型。每个操作都会先询问模型的策略:show 询问 read,store 询问 create,update 询问 update,destroy 询问 delete。index 不会询问,因为它不针对单条记录。
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_response这些路由的中间件,以逗号分隔。
示例
<KScript.Routes>
  <import path="controllers/people.ks.xml" as="people" />
  <Resource path="/people" to="people" middleware="auth" />
</KScript.Routes>
import KScript.Http
import "controllers/people.ks.xml" as people

Routes {
  Resource(path: '/people', to: people, middleware: auth)
}

#<KScript.Route>Route

一个可同时响应多个 HTTP 方法的路由。

包含语句。在 { } 中包含语句。仅限脚本。放在 <KScript.Routes>Routes 或 <Group>Group 内。

属性选项值说明
path 必需a string

The path, with parameters: /people/. {name?} is optional and {name*} takes the rest of the path.

methods 必填get | post | put | patch | delete | options | headHTTP 方法:[get, post]。
to函数:@name控制器:一个 <fn>(@show)或已导入脚本的一个函数(@people.show);在 XML 中为 to="people.show"。其参数按名称填充:路径参数、request、json、query、form、input、headers。控制器:一个 fn(@show)或已导入脚本的一个函数(@people.show);在 XML 中为 to="people.show"。其参数按名称填充:路径参数、request、json、query、form、input、headers。
viewa string改为从 views 文件夹返回此视图,并将路径参数作为变量。
redirecta string改为重定向到此地址。
status表达式重定向的状态码,默认为 302。
namea string路由的名称,用于 @route('name', ...)。
label_keya string路由显示标签的翻译键。
data表达式由已声明的记录类型构成的可选路由元数据。
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_response应用于此路由的中间件,以逗号分隔。
cana string控制器运行前模型策略必须允许的权限:can: update。系统会就控制器的类型化路径参数所指的记录进行询问,即 fn update(post: Post)。返回否时响应 403;被该权限拒绝的访客会被引导登录(API 则返回 401)。
wherea string参数规则:where: { id: int, slug: slug }(XML 中为 where="id: int, slug: slug")。规则可以是 number、int、alpha、alnum、slug、uuid 或正则表达式。
etagtrue 或 false设为 true 时,GET 响应会带上 ETag(正文的哈希值);当客户端已有该版本时,对 If-None-Match 返回 304 Not Modified。
cachea stringGET 响应的 Cache-Control 头:public, max-age=300,或仅为秒数(300 即公开缓存 5 分钟)、private、no-store……
示例
<KScript.Routes>
  <Route methods="get, post" path="/contact" to="contact.handle" />
</KScript.Routes>
import KScript.Http

Routes {
  Route(methods: [get, post], path: '/contact', to: @contact.handle)
}

#<KScript.Routes>Routes

一个路由文件:将 HTTP 方法和路径映射到控制器、视图和重定向。kscript serve 会加载 routes.ks.xml(或 routes.kscript)以及 api.ks.xml(位于 /api 下,返回 JSON 错误)。其 <import>、<fn> 和 <let> 子元素会在每个请求匹配路由之前运行。一个路由文件:将 HTTP 方法和路径映射到控制器、视图和重定向。kscript serve 会加载 routes.ks.xml(或 routes.kscript)以及 api.ks.xml(位于 /api 下,返回 JSON 错误)。其 import、fn 以及 $name = value 子元素会在每个请求匹配路由之前运行。

容纳语句。在 { } 中容纳语句。仅限脚本使用。

属性选项值说明
prefixa string放在文件中每条路由前面的路径前缀,例如 /v1。
middlewarelog | cors | auth | guest | csrf | signed | verified | throttle | etag | cache_control | cache_response文件中每条路由的中间件,用逗号分隔:内置中间件(log、cors、auth、throttle:30)或接收 ($request, $next) 的 <fn>。文件中每条路由的中间件,用逗号分隔:内置中间件(log、cors、auth、throttle:30)或接收 ($request, $next) 的 fn。
namea string加在每个路由名称前面的前缀,例如 admin。
apitrue 或 falsetrue:错误和未找到的路由将以 JSON 响应(api.* 文件本身就是 API 文件)。
示例
<KScript.Routes>
  <import path="controllers/people.ks.xml" as="people" />
  <Middleware use="log" />
  <Get path="/" view="home.html" />
  <Get path="/people/{id}" to="people.show" name="people.show" />
</KScript.Routes>
import KScript.Http
import "controllers/people.ks.xml" as people

Routes {
  Middleware(use: log)
  Get(path: '/', view: 'home.html')
  Get(path: "/people/{id}", to: @people.show, name: 'people.show')
}

命令

命令可通过 @ 调用,可用于任何表达式:@name(arguments)。

#@abort(status: number, message?) → any

以错误状态码中止请求,例如 @abort(404) 或 @abort(403, 'Admins only')。
参数接受
status 必填数字
message 可选任意值

参数可以使用名称:@abort(…, message: …)。

示例
<if $person == null><do>@abort(404)</do></if>
if $person == null {
  @abort(404)
}

#@asset_url(path: string) → string

使用 assets.base 以及项目配置中可选的 assets.manifest 映射构建公共资源 URL。
参数接受
path 必需string
示例
<echo>{ @asset_url('css/site.css') }</echo>
echo @asset_url('css/site.css')

#@etag(value, weak?: bool) → string

使用由某个值(例如记录的版本号或更新时间)生成的 ETag 标记响应。如果请求的 If-None-Match 已包含该值,请求将在此处以 304 Not Modified 结束,控制器的其余部分不再运行。
参数接受
value required任意值
weak 可选标志:weak 或 not weak

参数可以使用名称:@etag(…, weak: …)。

示例
<do>@etag($person.updated_at)</do>
@etag($person.updated_at)

#@redirect(to, status?: number) → http_response

重定向到另一个地址(默认为 302 Found;永久移动则为 301)。
参数接受
to 必填任意值
status 可选数字

参数可以使用名称:@redirect(…, status: …)。

示例
<return value="@redirect('/people')" />
return @redirect('/people')

#@redirect_back(default?, status?: number) → http_response

重定向回发出请求的页面(即其 Referer),这是表单验证未通过时的常见处理方式。只有本站页面才算数:来自其他站点的 Referer 或没有 Referer 时,将重定向到 default(省略时为 /)。
参数接受
default 可选任意值
status 可选数字

参数可以使用名称:@redirect_back(…, status: …)。

示例
<return value="@redirect_back('/posts')" />
return @redirect_back('/posts')

#@response(body?, status?: number, type?, headers?: map) → http_response

带有状态码和响应头的响应。映射或列表类型的正文会以 JSON 发送,文本以 HTML 发送,base64 值则作为原始字节发送。设置 type 可覆盖默认的 Content-Type。
参数接受
body 可选任意值
status 可选数字
type 可选任意值
headers 可选map

参数可以使用名称:@response(…, status: …, type: …)。

示例
<return value="@response($person, status: 201)" />
return @response($person, status: 201)

#@route(name: string(min: 1), params...) → string

返回指定命名路由的 URL。将路由名称作为非空文本传入,并为其路径参数提供值。例如,@route('people.show', id: 3) 返回 /people/3。额外的命名值会成为查询参数。
参数接受
name 必填string(min: 1)
params任意值,数量不限

参数可以使用名称:@route(…, params: …)。

示例
<a href="{ @route('people.show', id: $p.id) }">{ $p.name }</a>
a(href: @route('people.show', id: $p.id)) "{ $p.name }"

#@routes(kind?: page|http, data_type?: type, locale?: string) → list

列出已声明的 HTTP 路由和规范的基于文件的页面。可按类型或附加路由数据的声明类型进行筛选。
参数接受
kind 可选以下单词之一:page|http
data_type 可选type。一个已声明的记录类型,供路由的 data 选项使用。
locale 可选string

参数可以使用名称:@routes(…, data_type: …, locale: …)。

示例
<for each="route" in="@routes(kind: 'page')">
  <echo>{ $route.path }</echo>
</for>
for route in @routes(kind: 'page') {
  echo $route.path
}

#@route_is(name_or_pattern: string) → bool

如果当前请求匹配了该路由名称或规范模式,则为 true。
参数接受
name_or_pattern 必填string
示例
<if @route_is('posts.show')>
  <echo>Reading a post</echo>
</if>
if @route_is('posts.show') {
  echo 'Reading a post'
}

#@route_match(path: string, method?: string) → map

在不分派请求的情况下查询某个路径和方法。返回 status、route、parameters 以及 allowed methods。
参数接受
path 必需string
method 可选string

参数可以使用名称:@route_match(…, method: …)。

示例
<let name="found" value="@route_match('/posts/42')" />
<echo>{ $found.status } { $found.route } { $found.parameters }</echo>
$found = @route_match('/posts/42')
echo "{ $found.status } { $found.route } { $found.parameters }"

#@signed_url(to: string(min: 1), expires?: duration, params...) → string

仅此应用能生成的链接,用于取消订阅、验证和下载链接:路由名称及其参数(与 @route() 的参数相同),或路径,并附加签名。expires:使链接在该时长后失效,例如 7d。使用 signed 中间件或 $request.has_valid_signature 进行验证;修改链接的任何部分都会使其失效。
参数接受
to 必填string(min: 1)
expires 可选duration
params任意值,数量不限

参数可以命名:@signed_url(…, expires: …, params: …)。

示例
<a href="{ @signed_url('/unsubscribe', user: 42, expires: 7d) }">Unsubscribe</a>
a(href: @signed_url('/unsubscribe', user: 42, expires: 7d)) "Unsubscribe"

#@sitemap(paths: list, origin: string) → string

XML 站点地图,用于列出指定的公开路径,使用给定的 HTTP 或 HTTPS 源。
参数接受
paths 必需list
origin 必需string

参数可以命名:@sitemap(…, origin: …)。

示例
<let name="paths" value="['/', '/about', '/posts/hello']" />
<echo>{ @sitemap($paths, 'https://example.com') }</echo>
$paths = ['/', '/about', '/posts/hello']
echo @sitemap($paths, 'https://example.com')

#@view(name, data...) → http_response

从 views 文件夹(或 site 文件夹)渲染页面及其布局,并将命名的值作为变量传入:@view('people/show.html', person: $p)。可以省略扩展名。status:设置响应的状态码:@view('auth/login', error: $message, status: 422)。

参数可以命名:@view(…, data: …)。

示例
<return value="@view('people/show', person: $person)" />
return @view('people/show', person: $person)

方法

在 http_response 值上:->header()、->with_status()、->with_header()、->with_type()、->with_cookie()、->without_cookie()、->with_etag()、->with_cache()

变量

变量类型说明
$requestrequestHTTP 请求。在匹配的路由内,它还包含客户端 ip、解析后的 json、合并的 input 和 route name。