Getting Started
CLI
Request handling
- Routing
- Action Controller
- Resources
- Context
- Request Binding
- Middleware
- Error Handling
- Sessions
- Cookies
Frontend
Database
- Getting started with Pop
- Soda CLI
- Database Configuration
- Buffalo Integration
- Models
- Generators
- Migrations
- Fizz
- Mutations
- Querying
- Raw Queries
- Callbacks
- Scoping
- Associations and Relationships
- One to one associations
- One to many associations
Guides
- API Applications
- File Uploads
- Background Job Workers
- Mailers
- Tasks
- Local Authentication
- Third Party Authentication
- Events
- Go Modules
- Localization
- Logging
- Template Engines
- Testing
- Videos
Deploy
Request handling
Action Controller
In this chapter, you’ll learn how action controllers work; and how you can generate them using the built-in generators.
What is a Controller?
Controllers are the C part of the MVC pattern. They handle the logic given the router decision, and produce an appropriate response.
For instance, if you request the /
path of this website, the handler responsible of the home page will produce you the HTML home page as you see it. If you’re building a REST API, the controller will fetch or save some data, then ask (politely) the render engine to produce the appropriate response.
In Buffalo case, we commonly call controllers “actions”.
Define an Action
Buffalo’s actions (or controllers) are Handler functions:
func Home(c buffalo.Context) error {
return c.Render(200, r.HTML("home.html"))
}
In this example, we defined a “Home” action, and asked the rendering engine to produce an HTML page using the “home.html” template, and to reply with an HTTP 200 code.
Each action takes a buffalo.Context
as parameter: see Context to learn more about all you can do with it.
Generating Actions
Since writing actions boilerplate is quite redundant, Buffalo provides generator to help you.
$ buffalo g action --help
Generate new action(s)
Usage:
buffalo generate action [name] [handler name...] [flags]
Aliases:
action, a, actions
Flags:
-d, --dry-run dry run
-h, --help help for action
-m, --method string change the HTTP method for the generate action(s) (default "GET")
--skip-template skip generation of templates for action(s)
-v, --verbose verbosely run the generator
To generate actions for users
just type:
$ buffalo g a users show index create
This will generate the following files:
├── actions/
│ ├── users_test.go
│ └── users.go
│
└── templates/
└── users/
├── create.plush.html
├── index.plush.html
└── show.plush.html
Besides, Buffalo will register the user routes into actions/app.go
file:
// actions/app.go
app.GET("/users/show", UsersShow)
app.GET("/users/index", UsersIndex)
app.GET("/users/create", UsersCreate)
In some cases you will need to generate an action with an HTTP method different than GET
, for that case you can use the --method
flag, like in the following example:
$ buffalo g actions users message --method POST
In some other scenarios you will need to generate an action without generating an HTML template (e.g. for an API). To skip the generation of the HTML template for creating an action you can pass the --skip-template
flag to the generator, i.e:
$ buffalo g actions users update --skip-template
--api
flag. See APIs for further informations.
Destroying Actions
You can remove files generated by this generator by running:
$ buffalo destroy action users
Or in short form:
$ buffalo d a users