Controllers
Learn more about controllers in Caesar.
Overview
In Caesar, a controller is a struct that wraps HTTP handlers. Controllers are used to group related handlers together and to provide a way to share common functionality across multiple handlers.
Creating a controller
To create a controller, you can type the following command in your terminal:
caesar make:controller customers
This command will create a new controller file in the app/controllers
directory of your app, as well as registering the controller in the config/app.go
file.
// app/controllers/customers_controller.go
package controllers
// Uncomment the following import statement once you implement your first controller method.
// import (
// caesar "github.com/caesar-rocks/core"
// )
type CustomersController struct {
// Define dependencies here
}
func NewCustomersController() *CustomersController {
return &CustomersController{
// Inject dependencies here (like repositories, services, etc.)
}
}
// Define controller methods here
// func (c *CustomersController) Index(ctx *caesar.CaesarCtx) error {
// // Implement the controller method here
// return nil
// }