Routing
Framefox uses the@Route decorator to define URL patterns and map them to controller methods. The routing system supports path parameters, type hints, and automatic dependency injection.
Basic Route Definition
Use the@Route decorator on controller methods to define routes:
Route Parameters
The@Route decorator accepts the following parameters:
The URL path pattern for the route. Can include path parameters in curly braces like
/users/{user_id}A unique identifier for the route. Used for URL generation with
generate_url()List of HTTP methods accepted by this route (e.g.,
["GET"], ["POST"], ["GET", "POST"])Pydantic model for response validation (optional)
OpenAPI tags for grouping endpoints in API documentation
Path Parameters
Define dynamic URL segments using curly braces in the path and corresponding method parameters:Type Hints for Path Parameters
Framefox automatically converts path parameters based on their type hints:int- Integer valuesstr- String values (default)float- Floating-point numbersbool- Boolean values
Multiple HTTP Methods
Handle multiple HTTP methods in a single route:Route Naming Conventions
Follow these conventions for consistent route naming:- List/Index:
resource_listorresource_index(e.g.,user_list) - Show/Detail:
resource_detailorresource_show(e.g.,user_detail) - Create:
resource_create(e.g.,user_create) - Edit:
resource_edit(e.g.,user_edit) - Delete:
resource_delete(e.g.,user_delete) - API Endpoints: Prefix with
api_(e.g.,api_user_list)
Dependency Injection in Routes
Framefox automatically injects services into route methods based on type hints. The framework distinguishes between different parameter types:Automatic Service Injection
Services registered in the container are automatically injected:FastAPI Native Types
Framefox supports FastAPI’s native types without service injection:Request- HTTP request objectResponse- HTTP response objectBackgroundTasks- Background task schedulerWebSocket- WebSocket connectionCookie,Header,Query,Path,Body,Form,File- Parameter validators
Pydantic Models
Use Pydantic models for request body validation:Primitive Types
Primitive types are treated as query parameters:Response Models
Define response models for automatic OpenAPI documentation:API Tags
Group related endpoints using tags for better API documentation:URL Generation
Generate URLs from route names using thegenerate_url() method:
Best Practices
Use descriptive route names
Use descriptive route names
Choose route names that clearly indicate the resource and action. This makes URL generation and maintenance easier.
Keep paths RESTful
Keep paths RESTful
Follow REST conventions for resource paths:
GET /users- List all usersGET /users/{id}- Get single userPOST /users- Create userPUT /users/{id}- Update userDELETE /users/{id}- Delete user
Use type hints consistently
Use type hints consistently
Always provide type hints for path parameters and injected services. This enables automatic conversion and validation.
Separate API and web routes
Separate API and web routes
Use different controllers or prefixes for API endpoints vs. web pages. Tag API routes appropriately.
Next Steps
Responses
Learn about different response types
Forms
Handle form submissions and validation