-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: add document for built-in objects #914
Conversation
@dead-horse, thanks for your PR! By analyzing the history of the files in this pull request, we identified @popomore, @atian25 and @huacnlee to be potential reviewers. |
Codecov Report
@@ Coverage Diff @@
## master #914 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 28 28
Lines 669 669
=====================================
Hits 669 669 Continue to review full report at Codecov.
|
docs/source/zh-cn/basics/objects.md
Outdated
@@ -0,0 +1,227 @@ | |||
title: egg 内置基础对象 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Egg
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我把 egg 改成框架吧
docs/source/zh-cn/basics/objects.md
Outdated
title: egg 内置基础对象 | ||
--- | ||
|
||
在往下阅读之前,我们先初步介绍一下框架中内置的一些基础对象,包括从 [koa] 继承而来的 4 个对象(Application, Context, Request, Response) 以及框架扩展的一些对象(Helper, Controller, Service),在后续的文档阅读中我们会经常遇到它们。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Koa
docs/source/zh-cn/basics/objects.md
Outdated
|
||
## Context | ||
|
||
Context 是一个**请求级别的对象**,继承自 [koa.Context]。在每一次收到用户请求时,框架会实例化一个 Context 对象,这个对象封装了这次用户请求的信息,并提供了许多便捷的方法来获取请求参数或者设置响应信息。框架会将所有的 [service](./service.md) 挂载到 Context 实例上,一些插件也会将一些其他的方法和对象挂载到它上面([egg-sequelize] 会将所有的 model 挂载在 Context 上。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Service]
docs/source/zh-cn/basics/objects.md
Outdated
return class UserController extends app.Controller { | ||
fetch* () { | ||
const id = this.request.query.id; | ||
this.ctx.response.body = this.app.cache.get(id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里感觉可以稍微提下 this.ctx.body
和 this.ctx.response.body
等价,然后顺手提下 this.request.body
的区别。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
req 和 res 要提下么?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
req 和 res 先不提了
docs/source/zh-cn/basics/objects.md
Outdated
// app/controller/user.js | ||
|
||
// 从 egg 上获取 | ||
module.exports = UserController extends require('egg').Controller { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- require 另起一行吧。
- 注明下推荐哪种?
|
||
Helper 自身是一个类,有和 [Controller](#controller) 基类一样的属性,它也会在每次请求时进行实例化,因此 Helper 上的所有函数也能获取到当前请求相关的上下文信息。 | ||
|
||
### 获取方式 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
增加在模板里面使用 Helper 的说明?
docs/source/zh-cn/basics/objects.md
Outdated
title: egg 内置基础对象 | ||
--- | ||
|
||
在往下阅读之前,我们先初步介绍一下框架中内置的一些基础对象,包括从 [koa] 继承而来的 4 个对象(Application, Context, Request, Response) 以及框架扩展的一些对象(Helper, Controller, Service),在后续的文档阅读中我们会经常遇到它们。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Config 对象要提到么?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
感觉 config 没有什么特殊的, 在配置那一章节中已经描述好了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
但它也算是 egg 的内置基础对象吧,是不是可以稍微提下,给个链接过去。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
本来还准备加上 config 和 logger 的
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我等下都加上吧
@atian25 再看看吧 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
其他 +1,写的很赞
@@ -26,6 +26,7 @@ guide_toc: | |||
Get Start: 快速入门 | |||
Basics: 基础功能 | |||
Structure: 目录结构 | |||
Objects: 内置对象 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这篇有点 Concept 的感觉 😍
docs/source/zh-cn/basics/objects.md
Outdated
|
||
### 获取方式 | ||
|
||
最常见的 context 实例获取方式是在 [Middleware], [Controller] 以及 [Service] 中。[Controller] 中的获取方式在上面的例子中已经展示过了,在 [Service] 中获取和 [Controller] 中获取的方式一样,在 [Middleware] 中获取 Contenxt 实例则和 [koa] 框架在中间件中获取 Context 对象的方式一致。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
第一个 context 没大写,第二个拼写错误
docs/source/zh-cn/basics/objects.md
Outdated
fetch* () { | ||
const id = this.request.query.id; | ||
const user = this.app.cache.get(id); | ||
this.body = this.helper.formatUser(user); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this -> ctx
docs/source/zh-cn/basics/objects.md
Outdated
|
||
## Config | ||
|
||
我们推荐应用开发遵循配置和代码分离的原则,将一些需要硬编码的业务配置都放到配置文件中,同时配置文件支持各个不同的运行环境使用不同的配置,使用起来也非常方便,所有框架、插件和应用级别的配置都可以通过 Config 对象获取到,关于框架的配置,可以详细阅读[Config 配置](./config.md)章节。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Config 配置]
前面少个空格
docs/source/zh-cn/basics/objects.md
Outdated
// app/controller/user.js | ||
module.exports = app => { | ||
return class UserController extends app.Controller { | ||
fetch* () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* fetch()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dead-horse 没改全,搜索下
fixed |
docs/source/zh-cn/basics/objects.md
Outdated
// app/controller/user.js | ||
module.exports = app => { | ||
return class UserController extends app.Controller { | ||
fetch* () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dead-horse 没改全,搜索下
docs/source/zh-cn/basics/objects.md
Outdated
|
||
### 获取方式 | ||
|
||
最常见的 Context 实例获取方式是在 [Middleware], [Controller] 以及 [Service] 中。[Controller] 中的获取方式在上面的例子中已经展示过了,在 [Service] 中获取和 [Controller] 中获取的方式一样,在 [Middleware] 中获取 Contenxt 实例则和 [koa] 框架在中间件中获取 Context 对象的方式一致。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Contenxt
这个 typo 没改
额,再看看 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
其他 +1,没问题
docs/source/zh-cn/basics/objects.md
Outdated
}; | ||
``` | ||
|
||
和 [koa] 一样,在 Context 对象上,可以通过 `ctx.app` 访问到 Application 对象。以上面的 Controller 文件举例: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的链接是不是错了,底部的 [Koa]:
是大写的
docs/source/zh-cn/basics/objects.md
Outdated
|
||
### 获取方式 | ||
|
||
最常见的 Context 实例获取方式是在 [Middleware], [Controller] 以及 [Service] 中。[Controller] 中的获取方式在上面的例子中已经展示过了,在 [Service] 中获取和 [Controller] 中获取的方式一样,在 [Middleware] 中获取 Context 实例则和 [koa] 框架在中间件中获取 Context 对象的方式一致。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个 koa 也是
😢 |
这篇要赞~ |
docs/source/zh-cn/basics/objects.md
Outdated
}; | ||
``` | ||
|
||
在 Controller, Service 类中,可以通过 `this.app` 访问到 Application 对象。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以提一下这个是 controller 基类定义的,不然分不清 this.app 和 this.ctx.app
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
改成了 在继承于 Controller, Service 基类的实例中,可以通过
this.app 访问到 Application 对象。
|
||
## Context | ||
|
||
Context 是一个**请求级别的对象**,继承自 [koa.Context]。在每一次收到用户请求时,框架会实例化一个 Context 对象,这个对象封装了这次用户请求的信息,并提供了许多便捷的方法来获取请求参数或者设置响应信息。框架会将所有的 [Service] 挂载到 Context 实例上,一些插件也会将一些其他的方法和对象挂载到它上面([egg-sequelize] 会将所有的 model 挂载在 Context 上。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[egg-sequelize] 会将所有的 model 挂载在 Context 上
实际不是这样的
ctx.model.User
而不是直接挂的
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
最终都是�通过 ctx.model
来访问,和 service 一样吧?
|
||
最常见的 Context 实例获取方式是在 [Middleware], [Controller] 以及 [Service] 中。[Controller] 中的获取方式在上面的例子中已经展示过了,在 [Service] 中获取和 [Controller] 中获取的方式一样,在 [Middleware] 中获取 Context 实例则和 [Koa] 框架在中间件中获取 Context 对象的方式一致。 | ||
|
||
框架的 [Middleware] 同时支持 koa v1 和 koa v2 两种不同的中间件写法,根据不同的写法,获取 Context 实例的方式也稍有不同: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
koa -> Koa
|
||
## Request & Response | ||
|
||
Request 是一个**请求级别的对象**,继承自 [koa.Request]。封装了 node 原生的 http request 对象,提供了一系列辅助方法获取 HTTP 请求常用参数。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
node -> Node.js
http request -> HTTP Request
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
往后的类似地方也是需要修改
Checklist
npm test
passesAffected core subsystem(s)
Description of change