Action Controller

原文:https://gobuffalo.io/documentation/request_handling/actions/

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.

​ 控制器是 MVC 模式的 C 部分。它们处理路由器决策给出的逻辑,并产生适当的响应。

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.

​ 例如,如果您请求此网站的 / 路径,负责主页的处理程序将向您显示您看到的 HTML 主页。如果您正在构建 REST API,控制器将获取或保存一些数据,然后(礼貌地)要求渲染引擎生成适当的响应。

In Buffalo case, we commonly call controllers “actions”.

​ 在 Buffalo 案例中,我们通常将控制器称为“动作”。

Define an Action 定义动作

Buffalo’s actions (or controllers) are Handler functions:

​ Buffalo 的动作(或控制器)是处理程序函数:

1
2
3
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.

​ 在此示例中,我们定义了一个“主页”动作,并要求渲染引擎使用“home.html”模板生成 HTML 页面,并以 HTTP 200 代码回复。

Each action takes a buffalo.Context as parameter: see Context to learn more about all you can do with it.

​ 每个操作都将 buffalo.Context 作为参数:请参阅上下文以详细了解您可以使用它执行的所有操作。

Generating Actions 生成操作

Since writing actions boilerplate is quite redundant, Buffalo provides generator to help you.

​ 由于编写操作样板相当冗余,因此 Buffalo 提供了生成器来帮助您。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
$ 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:

​ 若要为 users 生成操作,只需键入:

1
$ 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:

​ 此外,Buffalo 会将用户路由注册到 actions/app.go 文件中:

1
2
3
4
5
// 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:

​ 在某些情况下,您需要生成一个 HTTP 方法与 GET 不同的操作,在这种情况下,您可以使用 --method 标志,如下例所示:

1
$ 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:

​ 在某些其他情况下,您需要生成一个不生成 HTML 模板的操作(例如,对于 API)。若要跳过为创建操作生成 HTML 模板,您可以将 --skip-template 标志传递给生成器,即:

1
$ buffalo g actions users update --skip-template

That’s the default behavior for applications generated with the --api flag. See APIs for further informations. 这是使用 --api 标志生成的应用程序的默认行为。有关更多信息,请参阅 API。

Destroying Actions 销毁操作

You can remove files generated by this generator by running:

​ 您可以通过运行以下命令来删除此生成器生成的文件:

1
$ buffalo destroy action users

Or in short form:

​ 或简短形式:

1
$ buffalo d a users

Next Steps 后续步骤

  • Resources - Define CRUD-like action bundles. 资源 - 定义类似 CRUD 的操作包。
  • Context - Learn more about Buffalo Context. 上下文 - 了解有关 Buffalo 上下文的更多信息。
最后修改 February 4, 2024: 更新 (c4156b3)