Controllers
Controllers are the HTTP boundary. They receive Echo contexts, parse and validate input, coordinate injected models or services, and choose an HTML, Inertia, JSON, redirect, or error response.
Explicit dependencies
Model APIs are values such as models.Products, not package globals. Fx supplies them to the controller constructor:
1type Products struct {
2 products models.Products
3}
4
5func NewProducts(products models.Products) Products {
6 return Products{products: products}
7}
Use etx.Request().Context() when calling models and services so cancellation crosses the HTTP boundary.
Render a Templ page
1func (p Products) Index(etx *echo.Context) error {
2 products, err := p.products.All(etx.Request().Context())
3 if err != nil {
4 return err
5 }
6 return hypermedia.RenderPage(etx, views.ProductsIndex(products))
7}
For Inertia, inject *inertia.Renderer and call its page, redirect, or location helpers. See Inertia. For JSON controllers, return Echo JSON responses and keep application types at the boundary.
Generate controllers
1andurel generate controller Product
2andurel generate controller Product index show
3andurel generate controller Dashboard overview
4andurel generate controller v1/User --api
Use --model-name when a controller is backed by a differently named model, --api for JSON handlers, or --inertia for pages using the configured frontend adapter.