Skip to content

Controllers

A controller is a class that answers one or more routes. Instead of putting every handler inline in routes/web.py, group related actions under app/http/controllers/ so each file stays small and testable.

Subclass Controller and write async methods. Return a Prism view, JSON, a redirect, or any shape the Responses page covers:

app/http/controllers/welcome_controller.py
from almasix.http import Controller
from almasix.prism import view
class WelcomeController(Controller):
async def index(self):
return view("welcome", {"title": "Almasix"})

Wire the action in a route file:

routes/web.py
from app.http.controllers.welcome_controller import WelcomeController
from almasix.routing import Route
Route.get("/", [WelcomeController, "index"])

Generate a stub:

terminal
python smith make:controller PostController
python smith make:model Post -c # model + plain controller
python smith make:model Post -mr # model + migration + resource controller

Nested namespaces work (python smith make:controller Admin/UserController) and create __init__.py files as needed.

Constructor and method dependencies are resolved from the application container — type-hint what you need and Almasix builds it:

app/http/controllers/demo_controller.py
from almasix.config import ConfigRepository
from almasix.http import Controller, Request
class DemoController(Controller):
def __init__(self, config: ConfigRepository) -> None:
self.config = config
async def with_config(self, request: Request) -> dict:
return {"app": self.config.get("app.name")}

Type-hint Request or a FormRequest subclass to receive the current request (validated when using FormRequest). Path parameters and bound models arrive as extra method arguments — see Routing.

For CRUD resources, name methods after the seven resource actions (index, create, store, show, edit, update, destroy) so Route.resource(...) can wire them automatically. Prefer one public method per intent. Almasix does not require invokable __call__ controllers — use an explicitly named method on the route.