Skip to content

Commit

Permalink
docs(controller): import base controller directly (#1771)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored Dec 1, 2017
1 parent 7ebfc9b commit a2788a8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 39 deletions.
38 changes: 18 additions & 20 deletions docs/source/en/basics/controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
37 changes: 18 additions & 19 deletions docs/source/zh-cn/basics/controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a2788a8

Please sign in to comment.