Skip to main content

resource

Macro resource 

Source
macro_rules! resource {
    ($path:expr, $($controller:ident)::+) => { ... };
    ($path:expr, $($controller:ident)::+, only: [$($action:ident),* $(,)?]) => { ... };
}
Expand description

Define RESTful resource routes with convention-over-configuration

Generates 7 standard routes following Rails/Laravel conventions from a controller module reference.

§Convention Mapping

MethodPathActionRoute Name
GET/usersindexusers.index
GET/users/createcreateusers.create
POST/usersstoreusers.store
GET/users/{id}showusers.show
GET/users/{id}/editeditusers.edit
PUT/users/{id}updateusers.update
DELETE/users/{id}destroyusers.destroy

§Basic Usage

routes! {
    resource!("/users", controllers::user),
}

§With Middleware

routes! {
    resource!("/admin", controllers::admin).middleware(AuthMiddleware),
}

§Subset of Actions

Use only: to generate only specific routes:

routes! {
    // Only index, show, and store - no create/edit forms, update, or destroy
    resource!("/posts", controllers::post, only: [index, show, store]),
}

§Path Naming

Route names are derived from the path:

  • /usersusers.index, users.show, etc.
  • /api/usersapi.users.index, api.users.show, etc.

§Compile Error

Fails to compile if path doesn’t start with ‘/’.