前端技術之:經過plop生成Controller的方法與步驟

Controller的生成

開發者能夠經過plop命令生成各類類型的控制器類(Normal、Restful、View), 如下是示例生成步驟。函數

  1. 執行如下命令:
plop controller

此時控制檯輸出:ui

? controller name please
  1. 輸入TestNormal並回車後,控制檯輸出如下提示,
? controller name please TestNormal
? which type of conroller do you want to create? (Use arrow keys)
❯ Normal Controller 
 Restful Controller 
 View Controller
  1. 根據須要選擇相應的控制器類型,並回車確認,控制檯輸出如下提示,
? which type of conroller do you want to create? Normal Controller
✔  ++ /server/controllers/TestNormalController.js

能夠生成的Controller包括Normal、Restful、View三種類型。rest

Normal Controller

用於生成通用的基礎Controller,生成的Controller類會繼承基礎Controller類。code

例如,orm

const Controller = require('../../lib/server/controller/controller');

class TestNormalController extends Controller {

}

module.exports = TestNormalController;

Restful Controller

用於建立基於Restful標準的Controller,生成的類的基類爲RestController,包括了基本的CRUD等函數。server

例如,繼承

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;
相關文章
相關標籤/搜索