Skip to main content
Kit provides a clean, Laravel-inspired routing system that lets you define routes declaratively using the 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 in src/routes.rs using the routes! macro. Each route specifies an HTTP method, a path, and a handler function:
The 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.
Access parameters in your controller using request.param():
For nested parameters:

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:
The parameter name (user) matches the route parameter placeholder ({user}). Kit will:
  1. Extract the value from the {user} route parameter
  2. Parse it as the primary key type (e.g., i32, String, UUID)
  3. Fetch the model from the database
  4. Return 404 Not Found if the model doesn’t exist
  5. 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 implements kit::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 the route() function:
This is especially useful for redirects:

Route Middleware

Apply middleware to specific routes using .middleware():
You can chain multiple middleware on a single route:
For more details on creating middleware, see the Middleware documentation.

Route Groups

Group related routes that share a common prefix and/or middleware using the group! macro inside routes!:

Group Syntax

The group! 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:
In this example:
  • /api/health has AuthMiddleware
  • /api/v1/users has AuthMiddleware
  • /api/v1/admin/stats has both AuthMiddleware AND AdminMiddleware

Middleware Inheritance

When groups are nested, middleware is inherited from parent to child. The execution order is:
  1. Parent group middleware (outermost)
  2. Child group middleware
  3. Route-specific middleware (innermost)
For the route /outer/inner/route, middleware executes in order: OuterMiddlewareInnerMiddlewareRouteMiddleware.

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

The fallback! 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.

File Organization

The standard file structure for routing in a Kit application:
src/routes.rs:

Summary