Skip to main content

Module binding

Module binding 

Source
Expand description

Route-model binding — an action parameter that is the looked-up model rather than its id.

ⓘ
// routes/api.rs
router.get("/posts/{post}", show).where_slug("post");

// app/http/controllers/post_controller.rs
pub async fn show(Bound(post): Bound<Post>) -> Result<Response> {
    Ok(Response::json(&post))
}

The action receives the model, not the id: the lookup, the 404 and the ? are all gone from the body, and what is left is the thing the action is actually about.

§Why it lives here

It needs a request, a container and a repository at once, and rainier-database is not allowed to know an HTTP request exists — that rule is what keeps a repository usable from a queue worker with no router in it. This crate is the one that already has both halves, the same reason DatabaseChannel is here.

Structs§

Bound
A model resolved from a route parameter.
BoundAs
A model resolved from a named route parameter.

Traits§

ParamKey
The name of a route parameter, as a type. See param_key!.