[TOC=2,4]
`think.controller.rest`?繼承自?[think.controller.base](https://thinkjs.org/zh-CN/doc/2.0/api_controller.html),用來處理 Rest 接口。
##### 使用 ES6 的語法繼承該類
~~~
export default class extends think.controller.rest {
}
~~~
##### 使用普通方式繼承該類
~~~
module.exports = think.controller("rest", {
})
~~~
### 屬性
#### controller._isRest
標識此 controller 對應的是 Rest 接口。如果在?`init`?方法里將該屬性設置為`false`,那么該 controller 不再是一個 Rest 接口。
#### controller._method
獲取 method 方式。默認從 http method 中獲取,但有些客戶端不支持發送 delete, put 之類的請求,所以可以設置為從 GET 里的一個參數獲取。
~~~
export default class extends think.controller.rest {
init(http){
super.init(http);
//設置 _method,表示從 GET 參數獲取 _method 字段的值
//如果沒有取到,則從 http method 中獲取
this._method = "_method";
}
}
~~~
#### controller.resource
當前 Rest 對應的 Resource 名稱。
#### controller.id
資源 ID
#### controller.modelInstance
資源對應 model 的實例。
### 方法
#### controller.__before()
可以在魔術方法`__before`中進行字段過濾,分頁,權限校驗等功能。
~~~
export default class extends think.controller.rest{
__before(){
//過濾 password 字段
this.modelInstance.field("password", true);
}
}
~~~
#### controller.getAction()
獲取資源數據,如果有 id,拉取一條,否則拉取列表。
~~~
//方法實現,可以根據需要修改
export default class extends think.controller.rest {
* getAction(){
let data;
if (this.id) {
let pk = yield this.modelInstance.getPk();
data = yield this.modelInstance.where({[pk]: this.id}).find();
return this.success(data);
}
data = yield this.modelInstance.select();
return this.success(data);
}
}
~~~
#### controller.postAction()
添加數據
~~~
//方法實現,可以根據需要修改
export default class extends think.controller.rest {
* postAction(){
let pk = yield this.modelInstance.getPk();
let data = this.post();
delete data[pk];
if(think.isEmpty(data)){
return this.fail("data is empty");
}
let insertId = yield this.modelInstance.add(data);
return this.success({id: insertId});
}
}
~~~
#### controller.deleteAction()
刪除數據
~~~
//方法實現,可以根據需要修改
export default class extends think.controller.rest {
* deleteAction(){
if (!this.id) {
return this.fail("params error");
}
let pk = yield this.modelInstance.getPk();
let rows = yield this.modelInstance.where({[pk]: this.id}).delete();
return this.success({affectedRows: rows});
}
}
~~~
#### controller.putAction()
更新數據
~~~
//方法實現,可以根據需要修改
export default class extends think.controller.rest {
* putAction(){
if (!this.id) {
return this.fail("params error");
}
let pk = yield this.modelInstance.getPk();
let data = this.post();
delete data[pk];
if (think.isEmpty(data)) {
return this.fail("data is empty");
}
let rows = yield this.modelInstance.where({[pk]: this.id}).update(data);
return this.success({affectedRows: rows});
}
}
~~~
#### controller.__call()
找不到方法時調用
~~~
export default class extends think.controller.rest {
__call(){
return this.fail(think.locale("ACTION_INVALID", this.http.action, this.http.url));
}
}
~~~
文檔地址:[https://github.com/75team/www.thinkjs.org/tree/master/view/zh-CN/doc/2.0/api_controller_rest.md](https://github.com/75team/www.thinkjs.org/tree/master/view/zh-CN/doc/2.0/api_controller_rest.md)
- 快速入門
- 介紹
- 創建項目
- 項目結構
- 代碼規范
- 升級指南
- 進階應用
- 模塊
- 控制器
- 視圖
- 配置
- 路由
- 模型
- 介紹
- 事務
- 關聯模型
- Mysql
- MongoDB
- SQLite
- Adapter
- 介紹
- Cache
- Session
- WebSocket
- Template
- 擴展功能
- thinkjs 命令
- 靜態資源訪問
- Middleware
- Service
- Cookie
- 錯誤處理
- 錯誤信息
- 數據校驗
- 國際化
- 路徑常量
- REST API
- 定時任務
- 線上部署
- 推薦模塊
- API
- think
- think.base
- think.http.base
- http
- controller
- rest controller
- model
- model.mongo
- middleware