Skip to content

Commit c4326d2

Browse files
dlhandsomeatian25
authored andcommitted
docs: added controller class example (#20)
1 parent 7c063fc commit c4326d2

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,16 @@ exports.validate = {
5656

5757
```js
5858
// app/controller/home.js
59-
exports.index = function* () {
60-
this.validate({ id: 'id' }); // will throw if invalid
61-
// or
62-
const errors = this.app.validator.validate({ id: 'id' }, this.request.body);
63-
};
59+
const Controller = require('egg').Controller;
60+
class HomeController extends Controller {
61+
async index() {
62+
const { ctx, app } = this;
63+
ctx.validate({ id: 'id' }); // will throw if invalid
64+
// or
65+
const errors = app.validator.validate({ id: 'id' }, ctx.request.body);
66+
}
67+
}
68+
module.exports = HomeController;
6469
```
6570

6671
### Extend Rules

0 commit comments

Comments
 (0)