controller 是ThinkKoa中的控制器基类。所有的控制器都必须继承 controller
或它的子类
const {controller} = require('thinkkoa');
module.exports = class extends controller {
//构造方法init代替constructor
init(ctx, app) {
}
}
ctx对象。
this.ctx
thinkkoa的实例, 是koa实例的扩展
this.app
空方法。执行当前控制器不存在的方法,自动调用。
//src/controller/index.js控制器
...
__empty(){
return this.write('action not found');
}
indexAction(){
return this.write('hello world');
}
...
//当访问 /index/index页面输出 'hello world'
//当访问 /index/aaa 页面输出 'action not found'
判断当前request是否GET请求。
if (this.isGet()) {
//当前请求为GET请求
}
判断当前request是否POST请求。
if (this.isPost()) {
//当前请求为POST请求
}
判断当前请求是否是传入的特定请求。
if (this.isMethod('get')) {
//当前请求为GET请求
}
判断当前request是否Ajax请求。
if (this.isAjax()) {
//当前请求为Ajax请求
}
判断当前request是否Pjax请求。
if (this.isPjax()) {
//当前请求为Pjax请求
}
判断当前request是否Jsonp请求。
if (this.isJsonp('callback')) {
//当前请求为Jsonp请求
}
获取或设置header内容。
this.header('Content-Type', 'text/plian'); //等同于 ctx.set('Content-Type', 'text/plian')
this.header('Content-Type'); //等同于 ctx.get('Content-Type')
获取或构造querystring参数。
//获取参数
let test = this.get('test') || '';
//构造参数
this.get('test', {aa: 1});
获取或构造post参数。
//获取参数
let test = this.post('test') || '';
//构造参数
this.post('test', {aa: 1});
querystring中同名key会被post值覆盖
获取参数,先从post参数中查找,如果不存在则从querstring中查找。let all = this.param();
let info = this.param('info') || {};
获取或构造上传的file对象。
//获取参数
let test = this.file('filename') || {};
//构造参数
this.file('test.txt', {...});
this.types('text/plian', 'utf-8');
获取request referrer。
let ref = this.referer();
ref = this.referer('http://baidu.com');
页面跳转。
this.redirect('/index');
this.redirect('http://baidu.com');
返回403禁止访问。
return this.deny();
依赖think_cookie中间件
获取或者设置cookie值。options包括项
//获取cookie
this.cookie('site');
//设置cookie
this.cookie('site', 'www.baidu.com');
如果options未传递,默认遵循中间件的配置。可在项目中间件配置文件中进行定义:
/**
* Middleware config
* @return
*/
module.exports = {
list: [], //加载的中间件列表
config: { //中间件配置
cookie: {
domain: '',
path: '/',
...
}
}
};
依赖think_session中间件
获取或设置session。
//获取session
this.session('user');
//写入session
this.session('user', {'username': 'test'});
//写入session,30s过期
this.session('user', {'username': 'test'}, 30);
依赖think_cache中间件
获取或设置缓存。
//在控制器中获取缓存
this.cache('user');
//在中间件或服务类中获取缓存需要使用控制器的this.app对象
app.cache('user');
//写入缓存
this.cache('user', {'username': 'test'});
//写入缓存,30s过期
this.cache('user', {'username': 'test'}, 30);
读取配置项。
//在控制器中获取配置
this.config('aa');
//在中间件或服务类中获取配置需要使用控制器的this.app对象
app.config('aa');
//获取项目配置 config/config.js
this.config('aa.bb'); // aa: {bb: 1}
//获取中间件配置 config/middleware.js
this.config('config.cache', 'middleware');
对ctx.body赋值进行功能封装。 注意控制器中的this.write方法和ctx.write最大的不同是输出内容后,会返回think.prevent()错误中断程序执行。
text/plain
utf-8
,在项目配置文件 src/config/config.js内可修改return this.write('content', 'text/plain'); //页面输出 content
response返回json格式数据。常用于API接口。
return this.json({aa: 111, bb: 222}); //页面输出 {"aa": 111, "bb":222}
response返回jsonp格式数据。用于回调前端函数。在jsonp返回值之前,request请求的时候需要传递callback函数名作为参数(http://host/index?callback=fun_name)
//http://host/index?callback=fun_name
return this.jsonp({dddddd: 1}); //页面输出 fun_name({"dddddd": 1})
在控制器逻辑执行成功时,response返回统一格式化json数据。常用于API接口。
return this.success('操作成功'); //页面输出 {"status":1,"errno":200,"errmsg":"操作成功","data":{}}
功能同success.
在控制器逻辑执行失败时,response返回统一格式化json数据。常用于API接口。
return this.error('操作失败'); //页面输出 {"status":0,"errno":500,"errmsg":"操作失败","data":{}}
功能同error.
依赖think_view中间件
在使用模板引擎渲染模板时候,向模板赋值,模板赋值数据对象保存在 this.tVar
。
this.assign('user', '张三');
//获取所有模板赋值变量
this.assign(); //返回 {"user": "张三"}
依赖think_view中间件
功能同assign.
依赖think_view中间件
传递文件物理路径,可以直接定位渲染模板
传递空值,框架自动根据 /模板路径配置/模块/控制器 查找模板
传递 /模块名/控制器名 也可以定位模板
调用模板引擎,渲染模板返回渲染后的内容.
let content = await this.compile('', {aa: 1});
依赖think_view中间件
传递文件物理路径,可以直接定位渲染模板
传递空值,框架自动根据 /模板路径配置/模块/控制器 查找模板
传递 /模块名/控制器名 也可以定位模板
渲染模板并输出内容,依赖中间件think_view
return this.render();
依赖think_view中间件
功能与render相同。