경로 매개변수
경로에서 중괄호로 묶인 부분은 경로 매개변수예요. 그 값은 같은 이름의 변수로 컨트롤러에 전달되므로, {id}는 $id가 돼요:
<!-- routes.kscript -->
<KScript.Routes>
<import path="controllers/people.kscript" as="people" />
<Get path="/people/{id}" to="people.page" />
<Get path="/archive/{year?}" to="people.index" />
<Get path="/files/{path*}" to="people.index" />
</KScript.Routes>// routes.kscript
import KScript.Http
import "controllers/people.kscript" as people
Routes {
Get(path: /people/{id}, to: @people.page)
Get(path: /archive/{year?}, to: @people.index)
Get(path: /files/{path*}, to: @people.index)
}{year?}는 선택 사항이고, {path*}는 나머지 경로를 모두 받아요. 경로는 슬래시 경로이므로 따옴표가 필요 없어요. path: /people/{id}와 path: "/people/{id}"는 같아요. 어느 쪽이든 {id}는 경로 매개변수이며 변수에서 값을 채우지 않아요.
매개변수 규칙
where:로 매개변수에 규칙을 지정할 수 있어요. 규칙을 어기는 요청은 그 경로와 일치하지 않으므로 다음 경로가 시도돼요:
<!-- routes.kscript -->
<KScript.Routes>
<import path="controllers/people.kscript" as="people" />
<Get path="/people/{id}" to="people.page" where="{ id: int }" />
<Get path="/posts/{slug}" to="people.index" where="{ slug: slug }" />
</KScript.Routes>// routes.kscript
import KScript.Http
import "controllers/people.kscript" as people
Routes {
Get(path: /people/{id}, to: @people.page, where: { id: int })
Get(path: /posts/{slug}, to: @people.index, where: { slug: slug })
}규칙은 number, int, alpha, alnum, slug, uuid이거나 따옴표로 감싼 정규 표현식이에요. XML에서는 같은 규칙을 where="id: int, slug: slug"로 작성해요.