Skip to main content
Actions in Kit are injectable service classes that encapsulate your application’s business logic. Inspired by Laravel’s single-action classes, actions promote clean code organization by separating business logic from controllers. The #[injectable] macro provides automatic dependency injection and singleton registration.

Generating Actions

The fastest way to create a new action is using the Kit CLI:
This command will:
  1. Create src/actions/create_user.rs with an action stub
  2. Update src/actions/mod.rs to export the new action

Action Structure

Actions are structs marked with #[injectable] that contain business logic:

The #[injectable] Macro

The #[injectable] macro provides powerful dependency injection:
  • Automatic registration: Actions are automatically registered as singletons in the container
  • Zero boilerplate: No manual container configuration required
  • Compile-time safety: Type-safe dependency resolution

Using Actions in Controllers

Resolve actions from the container using App::resolve():
The ? operator handles the case where the action isn’t registered, returning an appropriate error response.

Async Actions

Actions can be async for database operations and other I/O tasks:
Using async actions in controllers:

Actions with Dependencies

Actions can have dependencies injected via the #[inject] attribute:
Dependencies are automatically resolved from the container when the action is resolved.

When to Use Actions

Actions are ideal for:

Actions vs Controllers

File Organization

The standard file structure for actions:
src/actions/mod.rs:

Practical Examples

User Registration Action

Action with Return Types

Using Multiple Actions in a Controller

Summary