routes! macro. Routes map URLs to handler functions (controllers), support dynamic parameters, named routes for URL generation, and per-route middleware.
Defining Routes
Routes are defined insrc/routes.rs using the routes! macro. Each route specifies an HTTP method, a path, and a handler function:
routes! macro automatically generates a register() function that returns a configured Router.
HTTP Methods
Kit provides macros for all standard HTTP methods:Route Parameters
Dynamic segments in your URLs are defined using curly braces{param}. Kit also supports Express/Rails-style colon syntax :param which is automatically converted. Both syntaxes are fully supported:
Choose whichever syntax you prefer - Kit automatically converts
:param to {param} internally for compatibility with the underlying router.request.param():
Route Model Binding
Route model binding automatically resolves database models from route parameters. When you use a Model type as a handler parameter, Kit automatically fetches the model from the database using the route parameter value.Basic Usage
Simply use the Model type as a handler parameter with the#[handler] attribute:
user) matches the route parameter placeholder ({user}). Kit will:
- Extract the value from the
{user}route parameter - Parse it as the primary key type (e.g.,
i32,String,UUID) - Fetch the model from the database
- Return 404 Not Found if the model doesn’t exist
- Return 400 Bad Request if the parameter can’t be parsed
Route Definition
Define your route with a matching parameter name:Multiple Models
You can bind multiple models in a single handler:Mixed Parameters
Combine model binding with primitive parameters and form requests:Requirements
Route model binding works automatically for any model whose Entity implementskit::database::Model:
Route model binding supports any primary key type that implements
FromStr, including i32, i64, String, and uuid::Uuid.Opting Out
If you don’t want automatic model binding for a particular handler, simply don’t use the Model type as a parameter. Instead, extract the ID and query manually:Named Routes
Named routes allow you to generate URLs without hardcoding paths. Use.name() to assign a name to a route:
Naming Conventions
Follow Laravel-style naming conventions for consistency:URL Generation
Generate URLs from named routes using theroute() function:
Route Middleware
Apply middleware to specific routes using.middleware():
For more details on creating middleware, see the Middleware documentation.
Route Groups
Group related routes that share a common prefix and/or middleware using thegroup! macro inside routes!:
Group Syntax
Thegroup! macro takes a prefix and a block of routes:
Group with Middleware
Apply middleware to all routes in a group using.middleware():
Multiple Middleware
Chain multiple middleware on a group:Groups without Middleware
Groups can be used purely for URL prefixing without any middleware:Nested Groups
Groups can be nested arbitrarily deep. Nested groups inherit middleware from their parent groups, and prefixes are concatenated:/api/healthhasAuthMiddleware/api/v1/usershasAuthMiddleware/api/v1/admin/statshas bothAuthMiddlewareANDAdminMiddleware
Middleware Inheritance
When groups are nested, middleware is inherited from parent to child. The execution order is:- Parent group middleware (outermost)
- Child group middleware
- Route-specific middleware (innermost)
/outer/inner/route, middleware executes in order: OuterMiddleware → InnerMiddleware → RouteMiddleware.
Group Features
- Prefix: All routes in the group have the prefix prepended to their paths
- Named Routes: Routes inside groups can have names for URL generation
- Middleware: Apply middleware to all routes in the group at once
- Chaining: Multiple middleware can be chained on a group
- Nesting: Groups can be nested to any depth with inherited middleware
Fallback Route
Thefallback! macro allows you to define a custom handler that is called when no other routes match the request. This is useful for implementing custom 404 pages or catch-all handlers.
Basic Usage
Fallback Controller Example
Create a controller to handle unmatched routes:Fallback with Middleware
The fallback route supports middleware chaining, just like regular routes:Fallback with Inertia
You can also return Inertia responses for SPA-style 404 pages:If no fallback route is defined, Kit returns a default plain-text “404 Not Found” response.