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} method return
{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 name return
{Boolean} Whether is jsonp request.
name
{String} parameter name Get 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 name Get 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 name Get parameter value, first to read from POST, if return null, then get from GET.
name
{String} field name of upload file Get 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 name value
{String} header value Get 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 seconds Strong 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 host Get referrer。
name
{String} cookie name value
{String} cookie value options
{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 name value
{Mixed} session value return
{Promise} Read, set and clean session。
export default class extends think.controller.base {
* indexAction(){
// read session
let value = yield this.session("userInfo");
}
}
export default class extends think.controller.base {
* indexAction(){
//set session
yield this.session("userInfo", data);
}
}
export default class extends think.controller.base {
* indexAction(){
//清除当前用户的 session
yield this.session();
}
}
lang
{String} the setup of language asViewPath
{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 jump statusCode
{Number} status code, default is 302 Page jump.
name
{String | Object} variable name value
{Mixed} variable value Assign 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 path return
{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 {
* indexAction(){
// home/index_index.html
let content = yield this.fetch();
}
}
// suppose file path is /foo/bar/app/home/controller/index.js
export default class extends think.controller.base {
* indexAction(){
// home/index_detail.html
let content = yield this.fetch("detail");
}
}
// suppose file path is /foo/bar/app/home/controller/index.js
export default class extends think.controller.base {
* indexAction(){
// home/user_detail.html
let content = yield this.fetch("user/detail");
}
}
// suppose file path is /foo/bar/app/home/controller/index.js
export default class extends think.controller.base {
* indexAction(){
// admin/user_detail.html
let content = yield this.fetch("admin/user/detail");
}
}
// suppose file path is /foo/bar/app/home/controller/index.js
export default class extends think.controller.base {
* indexAction(){
// home/index_detail.xml
let content = yield this.fetch("detail.xml");
}
}
// suppose file path is /foo/bar/app/home/controller/index.js
export default class extends think.controller.base {
* indexAction(){
// /home/xxx/aaa/bbb/c.html
let content = yield this.fetch("/home/xxx/aaa/bbb/c.html");
}
}
templateFile
{String} template file path Output template content to browser side. strategy of finding template is the same as controller.fetch
.
data
{Mixed} content to output Using 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 content Json way to output.
status
{Number} status code, default is 404 Set status code.
status
{String} status code, default is 403 Deny current request.
data
{mixed} the output content encoding
{String} charset Output content.
data
{mixed} the output content encoding
{String} charset After output content, end current request.
type
{String} Content-Type charset
{Boolean} wheher append charset or not Set Content-Type。
filePath
{String} specified path of download file content-Type
{String} Content-Type fileName
{String} error file name Download 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 data message
{String} appended message Output 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 number errmsg
{String} error message data
{Mixed} extra data Output 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 key The execute time of send request, send with header.