Skip to main content
Kit controllers are async functions that handle HTTP requests and return responses. Following Laravel’s conventions, controllers organize your application’s request handling logic into dedicated modules, making your codebase clean and maintainable.

Generating Controllers

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

Controller Structure

Controllers are async functions decorated with #[handler] that take a Request and return a Response:

The Handler Attribute

All controller methods use the #[handler] attribute. This enables:
  • Automatic extraction and validation of request data
  • Clean integration with FormRequests for POST data validation
  • Future DX improvements like dependency injection
  • #[handler]: Required attribute that enables automatic parameter extraction
  • async fn: Controllers are asynchronous, allowing non-blocking I/O operations
  • Request: Contains all information about the incoming HTTP request
  • Response: An alias for Result<HttpResponse, HttpResponse>, enabling the ? operator
For handling POST data with validation, see Requests.

Path Parameter Injection

The #[handler] macro supports automatic extraction of path parameters directly as function arguments. This eliminates the need to manually call req.param().

Primitive Path Parameters

Declare path parameters as function arguments with primitive types:

Multiple Path Parameters

Extract multiple parameters in a single handler:

Supported Types

The parameter name in your function must match the route parameter name (e.g., {id} requires id: i32).

Route Model Binding

Route model binding automatically resolves database models from route parameters. If the model is not found, Kit automatically returns a 404 response.

Setting Up Route Binding

First, enable route binding for your model using the route_binding! macro:

Using Route Model Binding in Handlers

Now you can directly accept the model as a handler parameter:

Combining Models with Form Requests

Mix route model binding with form requests for update operations:

Error Responses

Flexibility

You have full control over how you access route parameters:

The Request Object

The Request struct provides Laravel-like access to request data:

Getting Route Parameters

Access dynamic URL segments defined in your routes:
For routes with multiple parameters:

Getting Headers

Access HTTP headers from the request:

Request Methods

Creating Responses

Controllers return responses using helper methods and macros:

JSON Responses

Text Responses

Setting Status Codes

For more response options, see the Responses documentation.

RESTful Controllers

Kit encourages organizing controllers following REST conventions:
Register these in your routes:

Error Handling in Controllers

Use the ? operator for clean error propagation:
For more error handling options, see the Responses documentation.

Dependency Injection

Use App::resolve() to inject dependencies from the container:

File Organization

The standard file structure for controllers:
src/controllers/mod.rs:
src/controllers/user.rs:

Practical Examples

API Controller with Validation

Controller with Redirects

Summary