-
Notifications
You must be signed in to change notification settings - Fork 4
Type‐safe routing
Kotlin Routing provides the Resources Plugin that allows you to implement type-safe routing. To accomplish this, you need to create a class that should act as a typed route and then annotate this class using the @Resource keyword. Note that the @Resource annotation has @Serializable behavior provided by the kotlinx.serialization library.
sourceSets {
commonMain.dependencies {
implementation("dev.programadorthi.routing:resources:$version")
}
}To install the Resources plugin to the application, pass it to the install function. The code snippets below show how to install Resources...
val router = routing {
install(Resources)
// ...
}The same as oficial docs Create resource classes
To define a route handler for a typed resource, you need to pass a resource class to a handle function. For example, a route handler below responds on the /articles path.
@Resource("/articles")
class Articles()
val router = routing {
install(Resources)
handle<Articles> { articles ->
// handle any call to Articles
}
}The example below shows how to define route handlers for the Articles resource created in Example: A resource for CRUD operations. Note that inside the route handler you can access the Article as a parameter and obtain its property values.
val router = routing {
install(Resources)
handle<Articles> { article ->
// Get all articles ...
}
handle<Articles.New> {
// Show a page with fields for creating a new article ...
}
handle<Articles>(method = RouteMethod("SAVE")) {
// Save an article ...
}
handle<Articles.Id> { article ->
// Show an article with id ${article.id} ...
}
handle<Articles.Id.Edit> { article ->
// Show a page with fields for editing an article ...
}
handle<Articles.Id>(method = RouteMethod("UPDATE")) { article ->
// Update an article ...
}
handle<Articles.Id>(method = RouteMethod("DELETE")) { article ->
// Delete an article ...
}
}The same as oficial docs Build links from resources