diff --git a/docs/source/en/basics/app-start.md b/docs/source/en/basics/app-start.md
index 198ebefeae..a93c859738 100644
--- a/docs/source/en/basics/app-start.md
+++ b/docs/source/en/basics/app-start.md
@@ -19,12 +19,12 @@ module.exports = app => {
};
```
-`cities` attribute has attached on the global `this.app`. It can be accessed in the controller,
+`cities` attribute has attached on the global `app`. It can be accessed in the controller,
```js
// app/controller/city.js
-module.exports = function*() {
- // this.app.cities // access `cities` property on the global `this.app`
+module.exports = function* (ctx) {
+ // ctx.app.cities // access `cities` property on the global `ctx.app`
}
```
diff --git a/docs/source/zh-cn/advanced/plugin.md b/docs/source/zh-cn/advanced/plugin.md
index 75b2a97bf4..a60fd77488 100644
--- a/docs/source/zh-cn/advanced/plugin.md
+++ b/docs/source/zh-cn/advanced/plugin.md
@@ -325,7 +325,7 @@ module.exports = {
// app/controller/post.js
module.exports = app => {
return class PostController extends app.Controller {
- *list () {
+ * list() {
const posts = yield this.app.mysql.query(sql, values);
},
};
@@ -366,7 +366,7 @@ exports.mysql = {
// app/controller/post.js
module.exports = app => {
return class PostController extends app.Controller {
- *list () {
+ * list() {
const posts = yield this.app.mysql.get('db1').query(sql, values);
},
};
@@ -395,7 +395,7 @@ module.exports = app => {
// app/controller/post.js
module.exports = app => {
return class PostController extends app.Controller {
- *list () {
+ * list() {
const posts = yield this.app.databse.query(sql, values);
},
};
diff --git a/docs/source/zh-cn/basics/app-start.md b/docs/source/zh-cn/basics/app-start.md
index afa84118de..c760afb3f7 100644
--- a/docs/source/zh-cn/basics/app-start.md
+++ b/docs/source/zh-cn/basics/app-start.md
@@ -22,8 +22,8 @@ module.exports = app => {
```js
// app/controller/city.js
-module.exports = function*() {
- // this.app.cities 在上面启动期间已经加载,可以直接使用
+module.exports = function* (ctx) {
+ // ctx.app.cities 在上面启动期间已经加载,可以直接使用
}
```
diff --git a/docs/source/zh-cn/basics/controller.md b/docs/source/zh-cn/basics/controller.md
index 65e21f44e3..b177224603 100644
--- a/docs/source/zh-cn/basics/controller.md
+++ b/docs/source/zh-cn/basics/controller.md
@@ -66,7 +66,7 @@ module.exports = {
- `this.ctx`: 当前请求的上下文 [Context](./extend.md#context) 对象的实例,通过它我们可以拿到框架封装好的处理当前请求的各种便捷属性和方法。
- `this.app`: 当前应用 [Application](./extend.md#application) 对象的实例,通过它我们可以拿到框架提供的全局对象和方法。
-- `this.service`:应用定义的 [Service](./service.md),通过它我们可以访问到抽象出的业务层。
+- `this.service`:应用定义的 [Service](./service.md),通过它我们可以访问到抽象出的业务层,等价于 `this.ctx.service` 。
- `this.config`:应用运行时的[配置项](./config.md)。
#### 自定义 Controller 基类
diff --git a/docs/source/zh-cn/basics/extend.md b/docs/source/zh-cn/basics/extend.md
index c8405071b5..e358bd7f6d 100644
--- a/docs/source/zh-cn/basics/extend.md
+++ b/docs/source/zh-cn/basics/extend.md
@@ -75,7 +75,8 @@ Context 指的是 Koa 的请求上下文,这是 **请求级别** 的对象,
### 访问方式
-- controller,middleware 中 `this` 就是 ctx,例如 `this.cookies.get('foo')`。
+- middleware 中 `this` 就是 ctx,例如 `this.cookies.get('foo')`。
+- controller 有两种写法,类的写法通过 `this.ctx`,方法的写法直接通过 `ctx` 入参。
- helper,service 中的 this 指向 helper,service 对象本身,使用 `this.ctx` 访问 context 对象,例如 `this.ctx.cookies.get('foo')`。
### 扩展方式
diff --git a/docs/source/zh-cn/basics/service.md b/docs/source/zh-cn/basics/service.md
index 1a2baff565..9b73803974 100644
--- a/docs/source/zh-cn/basics/service.md
+++ b/docs/source/zh-cn/basics/service.md
@@ -33,9 +33,9 @@ title: Service
- Service 文件必须放在 `app/service` 目录,可以支持多级目录,访问的时候可以通过目录名级联访问。
```js
- app/service/biz/user.js => this.service.biz.user
- app/service/sync_user.js => this.service.syncUser
- app/service/HackerNews.js => this.service.hackerNews
+ app/service/biz/user.js => ctx.service.biz.user
+ app/service/sync_user.js => ctx.service.syncUser
+ app/service/HackerNews.js => ctx.service.hackerNews
```
- 一个 Service 文件只能包含一个类, 这个类需要通过 `module.exports` 的方式返回。
@@ -62,10 +62,10 @@ module.exports = app => {
};
// app/controller/user.js
-exports.info = function* () {
- const userId = this.params.id;
- const userInfo = yield this.service.user.find(userId);
- this.body = userInfo;
+exports.info = function* (ctx) {
+ const userId = ctx.params.id;
+ const userInfo = yield ctx.service.user.find(userId);
+ ctx.body = userInfo;
};
// app/service/user.js
diff --git a/docs/source/zh-cn/core/error-handling.md b/docs/source/zh-cn/core/error-handling.md
index 1c067ab27b..cd56e5874f 100644
--- a/docs/source/zh-cn/core/error-handling.md
+++ b/docs/source/zh-cn/core/error-handling.md
@@ -21,13 +21,14 @@ try {
```js
// app/controller/jump.js
-const request = {};
-const config = yield this.service.trade.buy(request);
-// 下单后需要进行一次核对,且不阻塞当前请求
-setImmediate(() => {
- this.service.trade.check(request)
- .catch(err => this.logger.error(err));
-});
+exports.buy = function* (ctx) {
+ const request = {};
+ const config = yield ctx.service.trade.buy(request);
+ // 下单后需要进行一次核对,且不阻塞当前请求
+ setImmediate(() => {
+ ctx.service.trade.check(request).catch(err => ctx.logger.error(err));
+ });
+}
```
在这个场景中,如果 `service.trade.check` 方法中代码有问题,导致执行时抛出了异常,尽管框架会在最外层通过 `try catch` 统一捕获错误,但是由于 `setImmediate` 中的代码『跳出』了异步链,它里面的错误就无法被捕捉到了。因此在编写类似代码的时候一定要注意。
@@ -35,13 +36,15 @@ setImmediate(() => {
当然,框架也考虑到了这类场景,提供了 `ctx.runInBackground(scope)` 辅助方法,通过它又包装了一个异步链,所有在这个 scope 里面的错误都会统一捕获。
```js
-const request = {};
-const config = yield this.service.trade.buy(request);
-// 下单后需要进行一次核对,且不阻塞当前请求
-this.runInBackground(function* () {
- // 这里面的异常都会统统被 Backgroud 捕获掉,并打印错误日志
- yield this.service.trade.check(request);
-});
+exports.buy = function* (ctx) {
+ const request = {};
+ const config = yield ctx.service.trade.buy(request);
+ // 下单后需要进行一次核对,且不阻塞当前请求
+ ctx.runInBackground(function* () {
+ // 这里面的异常都会统统被 Backgroud 捕获掉,并打印错误日志
+ yield ctx.service.trade.check(request);
+ });
+}
```
**为了保证异常可追踪,必须保证所有抛出的异常都是 Error 类型,因为只有 Error 类型才会带上堆栈信息,定位到问题。**
diff --git a/docs/source/zh-cn/core/security.md b/docs/source/zh-cn/core/security.md
index 0c2537f686..0ef58313ea 100644
--- a/docs/source/zh-cn/core/security.md
+++ b/docs/source/zh-cn/core/security.md
@@ -116,7 +116,7 @@ console.log(`var foo = "${this.helper.sjs(foo)}";`);
```
还有一种情况,有时候我们需要在 JavaScript 中输出 json ,若未做转义,易被利用为 XSS 漏洞。框架提供了 `helper.sjson()` 宏做 json encode,会遍历 json 中的 key ,将 value 的值中,所有非白名单字符转义为 `\x` 形式,防止 XSS 攻击。同时保持 json 结构不变。
-若存在模板中输出一个 JSON 字符串给 JavaScript 使用的场景,请使用 `${this.helper.sjson(变量名)}` 进行转义。
+若存在模板中输出一个 JSON 字符串给 JavaScript 使用的场景,请使用 `{{ helper.sjson(变量名) }}` 进行转义。
**处理过程较复杂,性能损耗较大,请仅在必要时使用。**
@@ -124,7 +124,7 @@ console.log(`var foo = "${this.helper.sjs(foo)}";`);
```html
```
@@ -154,7 +154,7 @@ const value = `google