Struct axum_extra::routing::Resource
source · pub struct Resource<S = ()> { /* 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 {});
let app = Router::new().merge(users);
Implementations§
source§impl<S> Resource<S>
impl<S> Resource<S>
sourcepub fn named(resource_name: &str) -> Self
pub fn named(resource_name: &str) -> Self
Create a Resource
with the given name.
All routes will be nested at /{resource_name}
.
sourcepub fn index<H, T>(self, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
pub fn index<H, T>(self, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
Add a handler at GET /{resource_name}
.
sourcepub fn create<H, T>(self, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
pub fn create<H, T>(self, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
Add a handler at POST /{resource_name}
.
sourcepub fn new<H, T>(self, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
pub fn new<H, T>(self, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
Add a handler at GET /{resource_name}/new
.
sourcepub fn show<H, T>(self, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
pub fn show<H, T>(self, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
Add a handler at GET /{resource_name}/:{resource_name}_id
.
sourcepub fn edit<H, T>(self, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
pub fn edit<H, T>(self, handler: H) -> Selfwhere
H: Handler<T, S>,
T: 'static,
Add a handler at GET /{resource_name}/:{resource_name}_id/edit
.
Trait Implementations§
Auto Trait Implementations§
impl<S> Freeze for Resource<S>
impl<S = ()> !RefUnwindSafe for Resource<S>
impl<S> Send for Resource<S>
impl<S> Sync for Resource<S>
impl<S> Unpin for Resource<S>
impl<S = ()> !UnwindSafe for Resource<S>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more