- [Inheritence with ES6:](#inheritence-with-es6)
- [Inheritence With Normal Way](#inheritence-with-normal-way)
The think.http.base
class inherit from think.base, it is the base class that contains all of the operations related to http. Middleware, controller and view class are all inherit from this class.
export default class extends think.http.base {
/**
* initial function, will automatically invoked while instacing, didn't need constructor.
* @return {}
*/
init(){
}
}
module.exports = think.Class(think.http.base, {
init: function(){
}
});
Packaged http object, contained methods and function to be seen in API -> http.
name
{String} config filevalue
{Mixed} config valueRead or setup config, it is read config when value assigned to undefined
, otherwise it is setup config.
This function can not only read system default config, but also read project config.
Notice
: Don't setup with request user's information, it will be covered by other user.
export default class extends think.controller.base {
indexAction(){
// get config value
let value = this.config('name');
}
}
controller
{Object | String} controller instanceaction
{String} action namereturn
{Promise} Invoke action in controller, return a Promise, invoke __before
and __after
automcatically.
If controller is a string, it will automactically to find this controller.
// invoke action of current module's controller
export default class extends think.controller.base {
async indexAction(){
// invoke defail function in user controller
let value = await this.action('user', 'detail');
}
}
// invoke action in cross module's controller
export default class extends think.controller.base {
async indexAction(){
// invoke detail function of user controller in admin module
let value = await this.action('admin/user', 'detail');
}
}
name
{String} cache namevalue
{Mixed | Function} cache valueoptions
{Object} cache options, more informtion in cache config.Read or set cache, it is read cache when assign value
to undefined
, otherwise, it is setup cache. default type is file
.
export default class extends think.controller.base {
async indexAction(){
// get cache
let value = await this.cache('name');
}
}
When value
is function, it means read cache, if cache's value didn't exist, it will invoke this function, and assign the returning value to cache and return the value.It is very useful to avoid a trouble which judge the cache is exist when developing project and then to read cache and set cache in other place.
export default class extends think.controller.base {
async indexAction(){
// setup cache, when cache didn't exist, it invoke function automatically, and set cache at the same time
let value = await this.cache('name', () => {
return this.model('user').select();
});
}
}
Setup cache and modify the type:
export default class extends think.controller.base {
async indexAction(){
// setup cache, cache type is redis
await this.cache('name', 'value', {
type: 'redis'
});
}
}
event
{String} event namedata
{Mixed} argumentreturn
{Promise}Execute hook event, a hook has some middleware, it will execute those middleware orderly.
Hook event can be assigned in src/common/config/hook.js
, also it can be registered with think.hook.
export default class extends think.controller.base {
async indexAction(){
let result = await this.hook('parse_data');
}
}
name
{String} model nameoptions
{Object} options, more detail seen in database configreturn
{Object} model instanceGet the instance of model, which is instance of current module by default, it also can get instance of other module.
export default class extends think.controller.base {
indexAction(){
// get instance of user model in current module
let model = this.model('user');
// get instance of article model in admin module
let model1 = this.model('admin/article');
// get instance of test model in current module, and it is sqlite database
let model2 = this.model('test', {
type: 'sqlite' // setup type of database to sqlite, more detail to see in database config
})
}
}
name
{String} controller namereturn
{Object} controller instanceGet the instance of Controller, if cannot find Controller, it will report errors.
export default class extends think.controller.base {
indexAction(){
// get instance of user controller in current module
let controller = this.controller('user');
// get instance of user controller in admin module
let controller1 = this.controller('admin/user');
}
}
name
{String} service namereturn
{Class} Get the service, it maybe return a class, or an object, so it will not instance automatically.
export default class extends think.controller.base {
indexAction(){
// get the service
let service = this.service('user');
// get instance of service
let instance = new service(...args);
// get user service in admin module
let service1 = this.service('admin/user');
}
}