The think.controller.rest
class inherit from think.controller.base, used for handle Rest API.
export default class extends think.controller.rest {
}
module.exports = think.controller('rest', {
})
Identify this controller is Rest api. if in init
function, it assigned to false
, and this controller is not a Rest interface no more.
The way to get method, by default read from http method, but some client don't support send some request type like DELETE, PUT, so it can set to get from GET parameter.
export default class extends think.controller.rest {
init(http){
super.init(http);
// set _method, means get _method field value from GET parameters
// if is null, it will get from http method
this._method = '_method';
}
}
The Resource name of current Rest
Resource ID
The instance model of resource.
It can do some operate like filter field, pagination, access control in magic function __before
.
export default class extends think.controller.rest{
__before(){
// filter password field
this.modelInstance.field('password', true);
}
}
Get resource data, if id exist, then get one, or get the list.
// function implementation, it can been modified if need.
export default class extends think.controller.rest {
async getAction(){
let data;
if (this.id) {
let pk = await this.modelInstance.getPk();
data = await this.modelInstance.where({[pk]: this.id}).find();
return this.success(data);
}
data = await this.modelInstance.select();
return this.success(data);
}
}
Add data.
// function implementation, it can been modified if need.
export default class extends think.controller.rest {
async postAction(){
let pk = await this.modelInstance.getPk();
let data = this.post();
delete data[pk];
if(think.isEmpty(data)){
return this.fail('data is empty');
}
let insertId = await this.modelInstance.add(data);
return this.success({id: insertId});
}
}
Delete data.
// function implementaion, it can been modified if need.
export default class extends think.controller.rest {
async deleteAction(){
if (!this.id) {
return this.fail('params error');
}
let pk = await this.modelInstance.getPk();
let rows = await this.modelInstance.where({[pk]: this.id}).delete();
return this.success({affectedRows: rows});
}
}
Update data.
// function implementaion, it can been modified if need.
export default class extends think.controller.rest {
async putAction(){
if (!this.id) {
return this.fail('params error');
}
let pk = await this.modelInstance.getPk();
let data = this.post();
delete data[pk];
if (think.isEmpty(data)) {
return this.fail('data is empty');
}
let rows = await this.modelInstance.where({[pk]: this.id}).update(data);
return this.success({affectedRows: rows});
}
}
Invoked when cannot find function
export default class extends think.controller.rest {
__call(){
return this.fail(think.locale('ACTION_INVALID', this.http.action, this.http.url));
}
}