diff --git a/docs/source/en/basics/controller.md b/docs/source/en/basics/controller.md index 57fa3fce6d..f1f5e48b94 100644 --- a/docs/source/en/basics/controller.md +++ b/docs/source/en/basics/controller.md @@ -82,36 +82,34 @@ The defined Controller class initializes a new object for every request accessin Defining a Controller class helps us not only abstract the Controller layer codes better(e.g. universal routines can be abstracted as private) but also encapsulate methods that are widely used in the application by defining a customized Controller base class. -In [App Start](./app-start.md), the application is allowed to define a customized Controller base class, and methods in base class can be used by Controllers in `app/controller` therefore. - ```js -// app.js -module.exports = app => { - class CustomController extends app.Controller { - get user() { - return this.ctx.session.user; - } +// app/core/base_controller.js +const { Controller } = require('egg'); +class BaseController extends Controller { + get user() { + return this.ctx.session.user; + } - success(data) { - this.ctx.body = { - success: true, - data, - }; - } + success(data) { + this.ctx.body = { + success: true, + data, + }; + } - notFound(msg) { - msg = msg || 'not found'; - this.ctx.throw(404, msg); - } + notFound(msg) { + msg = msg || 'not found'; + this.ctx.throw(404, msg); } - app.Controller = CustomController; } +module.exports = BaseController; ``` -Now we can use base class' methods when defining Controllers for the application: +Now we can use base class' methods by extends from `BaseController`: ```js //app/controller/post.js +const Controller = require('../core/base_controller'); class PostController extends Controller { async list() { const posts = await this.service.listByUser(this.user); diff --git a/docs/source/zh-cn/basics/controller.md b/docs/source/zh-cn/basics/controller.md index c86c691375..b6ee60c768 100644 --- a/docs/source/zh-cn/basics/controller.md +++ b/docs/source/zh-cn/basics/controller.md @@ -82,36 +82,35 @@ module.exports = app => { 按照类的方式编写 Controller,不仅可以让我们更好的对 Controller 层代码进行抽象(例如将一些统一的处理抽象成一些私有方法),还可以通过自定义 Controller 基类的方式封装应用中常用的方法。 -在[启动自定义](./app-start.md)中,应用可自己定义 Controller 基类,这样在 `app/controller` 中编写 Controller 时就可以使用到定义在基类上的这些方法了。 ```js -// app.js -module.exports = app => { - class CustomController extends app.Controller { - get user() { - return this.ctx.session.user; - } +// app/core/base_controller.js +const { Controller } = require('egg'); +class BaseController extends Controller { + get user() { + return this.ctx.session.user; + } - success(data) { - this.ctx.body = { - success: true, - data, - }; - } + success(data) { + this.ctx.body = { + success: true, + data, + }; + } - notFound(msg) { - msg = msg || 'not found'; - this.ctx.throw(404, msg); - } + notFound(msg) { + msg = msg || 'not found'; + this.ctx.throw(404, msg); } - app.Controller = CustomController; } +module.exports = BaseController; ``` -此时在编写应用的 Controller 时,可以直接使用基类上的方法: +此时在编写应用的 Controller 时,可以继承 BaseController,直接使用基类上的方法: ```js //app/controller/post.js +const Controller = require('../core/base_controller'); class PostController extends Controller { async list() { const posts = await this.service.listByUser(this.user);