The think.controller.base class inherit from think.http.base class, controllers in project need to inherit it.
export default class extends think.controller.base {
indexAction(){
}
}
module.exports = think.controller({
indexAction(){
}
})
Passed http object.
return {String}Get user ip of current request, it is equal to http.ip.
export default class extends think.controller.base {
indexAction(){
let ip = this.ip();
}
}
return {String}Get type of current request, and convert to lowercase.
export default class extends think.controller.base {
indexAction(){
let method = this.method(); //get or post ...
}
}
method {String} methodreturn {Boolean}Judge type of current request is named types.
return {Boolean}Judge is GET request or not.
return {Boolean}Judge is POST request.
method {String}return {Boolean}Judge is Ajax request, if named method, then as same as the type of request.
export default class extends think.controller.base {
indexAction(){
// is ajax and request type is POST
let isAjax = this.isAjax('post');
}
}
return {Boolean}Whether is websocket request or not.
return {Boolean}Whether is run in command mode or not.
callback {String} callback namereturn {Boolean}Whether is jsonp request.
name {String} parameter nameGet parameter of GET.
export default class extends think.controller.base {
indexAction(){
// get a parameter
let value = this.get('xxx');
// get all parameter
let values = this.get();
}
}
name {String} parameter nameGet parameter of POST data.
export default class extends think.controller.base {
indexAction(){
// get a value of parameter
let value = this.post('xxx');
// get all parameter of POST
let values = this.post();
}
}
name {String} parameter nameGet parameter value, first to read from POST, if return null, then get from GET.
name {String} field name of upload fileGet uploaded file, return value is a object, contains these method below:
{
fieldName: 'file', // field name
originalFilename: filename, // original file name
path: filepath, // path of temp store file, need to move this path when using, or exists until request ends.
size: 1000 // file size
}
If file not exist, this returning is an empty object {}.
name {String} header namevalue {String} header valueGet or set header。
export default class extends think.controller.base {
indexAction(){
let accept = this.header('accept'); // get header
this.header('X-NAME', 'thinks'); // set header
}
}
time {Number} expires time, the unit is secondsStrong cache, set Cache-Control and Expires header information.
export default class extends think.controller.base {
indexAction(){
this.expires(86400); // set expire time to one day.
}
}
Get userAgent。
referrer {Boolean} whether only need hostGet referrer。
name {String} cookie namevalue {String} cookie valueoptions {Object}Get or set cookie。
export default class extends think.controller.base {
indexAction(){
// get value of cookie
let value = this.cookie('think_name');
}
}
export default class extends think.controller.base {
indexAction(){
// get value of cookie
this.cookie('think_name', value, {
timeout: 3600 * 24 * 7 // expires time is one week
});
}
}
name {String} session namevalue {Mixed} session valuereturn {Promise}Read, set and clean session。
export default class extends think.controller.base {
async indexAction(){
// read session
let value = await this.session('userInfo');
}
}
export default class extends think.controller.base {
async indexAction(){
//set session
await this.session('userInfo', data);
}
}
export default class extends think.controller.base {
async indexAction(){
//清除当前用户的 session
await this.session();
}
}
lang {String} the setup of languageasViewPath {Boolean} whether add a directory layer for language template.Read or set language.
key {String} Based on language to get the language version.
url {String} the url to jumpstatusCode {Number} status code, default is 302Page jump.
name {String | Object} variable namevalue {Mixed} variable valueAssign variable into template.
export default class extends think.controller.base {
indexAction(){
// single assign
this.assign('title', 'thinkjs');
// multi-assign
this.assign({
name: 'xxx',
desc: 'yyy'
})
}
}
templateFile {String} tempate file pathreturn {Promise}Get the parsed template content.
// suppose the file path is /foo/bar/app/home/controller/index.js
export default class extends think.controller.base {
async indexAction(){
// home/index_index.html
let content = await this.fetch();
}
}
// suppose file path is /foo/bar/app/home/controller/index.js
export default class extends think.controller.base {
async indexAction(){
// home/index_detail.html
let content = await this.fetch('detail');
}
}
// suppose file path is /foo/bar/app/home/controller/index.js
export default class extends think.controller.base {
async indexAction(){
// home/user_detail.html
let content = await this.fetch('user/detail');
}
}
// suppose file path is /foo/bar/app/home/controller/index.js
export default class extends think.controller.base {
async indexAction(){
// admin/user_detail.html
let content = await this.fetch('admin/user/detail');
}
}
// suppose file path is /foo/bar/app/home/controller/index.js
export default class extends think.controller.base {
async indexAction(){
// home/index_detail.xml
let content = await this.fetch('detail.xml');
}
}
// suppose file path is /foo/bar/app/home/controller/index.js
export default class extends think.controller.base {
async indexAction(){
// /home/xxx/aaa/bbb/c.html
let content = await this.fetch('/home/xxx/aaa/bbb/c.html');
}
}
templateFile {String} template file pathOutput template content to browser side. strategy of finding template is the same as controller.fetch.
data {Mixed} content to outputUsing the way of jsonp to output content, after getting callback's name and security filter then output.
export default class extends think.controller.base {
indexAction(){
this.jsonp({name: 'thinkjs'});
//writes
'callback_fn_name({name: "thinkjs"})'
}
}
data {Mixed} the output contentJson way to output.
status {Number} status code, default is 404Set status code.
status {String} status code, default is 403Deny current request.
data {mixed} the output contentencoding {String} charsetOutput content.
data {mixed} the output contentencoding {String} charsetAfter output content, end current request.
type {String} Content-Typecharset {Boolean} wheher append charset or notSet Content-Type。
filePath {String} specified path of download filecontent-Type {String} Content-TypefileName {String} error file nameDownload file.
export default class extends think.controller.base {
indexAction(){
let filePath = think.RESOUCE_PATH + '/a.txt';
// auto identify Content-Type, save file to a.txt
this.download(filePath);
}
}
export default class extends think.controller.base {
indexAction(){
let filePath = think.RESOUCE_PATH + '/a.log';
// auto identify Content-Type, save file to b.txt
this.download(filePath, 'b.txt');
}
}
export default class extends think.controller.base {
indexAction(){
let filePath = think.RESOUCE_PATH + '/a.log';
// specify Content-Type to text/html, save file to b.txt
this.download(filePath, 'text/html', 'b.txt');
}
}
data {Mixed} the output datamessage {String} appended messageOutput an normal formatted data, often after operate success.
http.success({name: 'thinkjs'});
//writes
{
errno: 0,
errmsg: '',
data: {
name: 'thinkjs'
}
}
Client can based on error is 0 or not to judge current request is success.
errno {Number} error numbererrmsg {String} error messagedata {Mixed} extra dataOutput an unusual formatted data, normally after operate failed.
Notice: field name errno and errmsg can been modified in config.
http.fail(100, 'fail')
//writes
{
errno: 100,
errmsg: 'fail',
data: ''
}
In this way, client will get detail error number and error message, then show message according to the need.
Notice: filed name errno and errmsg can been modified in config.
name {String} header keyThe execute time of send request, send with header.