Skip to content

Type‐safe routing

Thiago Santos edited this page Aug 13, 2024 · 4 revisions

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")
    }
}

Install Resources

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)
    // ...
}

Create resource classes

The same as oficial docs Create resource classes

Define route handlers

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 ...
    }
}

Build links from resources

The same as oficial docs Build links from resources

Clone this wiki locally