Controller的生成
开发者可以通过plop命令生成各种类型的控制器类(Normal、Restful、View), 以下是示例生成步骤。
- 执行以下命令:
plop controller
此时控制台输出:
? controller name please
-
输入TestNormal并回车后,控制台输出以下提示,
? controller name please TestNormal
? which type of conroller do you want to create? (Use arrow keys)
❯ Normal Controller
Restful Controller
View Controller
- 根据需要选择相应的控制器类型,并回车确认,控制台输出以下提示,
? which type of conroller do you want to create? Normal Controller
✔ ++ /server/controllers/TestNormalController.js
可以生成的Controller包括Normal、Restful、View三种类型。
Normal Controller
用于生成通用的基础Controller,生成的Controller类会继承基础Controller类。
例如,
const Controller = require('../../lib/server/controller/controller');
class TestNormalController extends Controller {
}
module.exports = TestNormalController;
Restful Controller
用于创建基于Restful标准的Controller,生成的类的基类为RestController,包括了基本的CRUD等函数。
例如,
const RestController = require('../../lib/server/controller/rest_controller');
class RestRestController extends RestController {
index() {
}
create() {
}
store() {
}
show() {
}
edit() {
}
update() {
}
destroy() {
}
}
module.exports = RestRestController;
View Controller
用于生成基本模板视图的控制器类,其生成的类的基类为ViewController。
const ViewController = require('../../lib/server/controller/view_controller');
class ViewViewController extends ViewController {
view() {
}
}
module.exports = ViewViewController;