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.
Basic controllers
Section titled “Basic controllers”Subclass Controller and write async methods. Return a Prism view, JSON, a
redirect, or any shape the Responses page covers:
from almasix.http import Controllerfrom almasix.prism import view
class WelcomeController(Controller): async def index(self): return view("welcome", {"title": "Almasix"})Wire the action in a route file:
from app.http.controllers.welcome_controller import WelcomeController
from almasix.routing import Route
Route.get("/", [WelcomeController, "index"])Generate a stub:
python smith make:controller PostControllerpython smith make:model Post -c # model + plain controllerpython smith make:model Post -mr # model + migration + resource controllerNested namespaces work (python smith make:controller Admin/UserController)
and create __init__.py files as needed.
Dependency injection
Section titled “Dependency injection”Constructor and method dependencies are resolved from the application container — type-hint what you need and Almasix builds it:
from almasix.config import ConfigRepositoryfrom 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.
Resource-style methods
Section titled “Resource-style methods”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.