pub struct Resource<B = Body> { /* private fields */ }
Expand description

A resource which defines a set of conventional CRUD routes.

Example

use axum::{Router, routing::get, extract::Path};
use axum_extra::routing::{RouterExt, Resource};

let users = Resource::named("users")
    // Define a route for `GET /users`
    .index(|| async {})
    // `POST /users`
    .create(|| async {})
    // `GET /users/new`
    .new(|| async {})
    // `GET /users/:users_id`
    .show(|Path(user_id): Path<u64>| async {})
    // `GET /users/:users_id/edit`
    .edit(|Path(user_id): Path<u64>| async {})
    // `PUT or PATCH /users/:users_id`
    .update(|Path(user_id): Path<u64>| async {})
    // `DELETE /users/:users_id`
    .destroy(|Path(user_id): Path<u64>| async {})
    // Nest another router at the "member level"
    // This defines a route for `GET /users/:users_id/tweets`
    .nest(Router::new().route(
        "/tweets",
        get(|Path(user_id): Path<u64>| async {}),
    ))
    // Nest another router at the "collection level"
    // This defines a route for `GET /users/featured`
    .nest_collection(
        Router::new().route("/featured", get(|| async {})),
    );

let app = Router::new().with(users);

Implementations

Create a Resource with the given name.

All routes will be nested at /{resource_name}.

Add a handler at GET /{resource_name}.

Add a handler at POST /{resource_name}.

Add a handler at GET /{resource_name}/new.

Add a handler at GET /{resource_name}/:{resource_name}_id.

Add a handler at GET /{resource_name}/:{resource_name}_id/edit.

Add a handler at PUT or PATCH /resource_name/:{resource_name}_id.

Add a handler at DELETE /{resource_name}/:{resource_name}_id.

Nest another route at the “member level”.

The routes will be nested at /{resource_name}/:{resource_name}_id.

Nest another route at the “collection level”.

The routes will be nested at /{resource_name}.

Trait Implementations

Formats the value using the given formatter. Read more

Get the routes.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more