Vanilla 的 controller

vanilla 的 controller 是业务处理的关键,基本的用法请参考 快速开始

关于 Controller

Vanilla 的 Controller 可以是任何普通的 LUA 包,只不过导入的方法被用作处理请求的 Action。如下示例:

  1. local IndexController = {}
  2. -- curl http://localhost:9110
  3. function IndexController:index()
  4. local view = self:getView()
  5. local p = {}
  6. p['vanilla'] = 'Welcome To Vanilla...' .. user_service:get()
  7. p['zhoujing'] = 'Power by Openresty'
  8. -- view:assign(p)
  9. do return view:render('index/index.html', p) end
  10. return view:display()
  11. end
  12. -- curl http://localhost:9110/index/action_b
  13. function IndexController:action_b()
  14. return 'index->action_b'
  15. end
  16. return IndexController

更面向对象的 Controller

Vanilla 支持使用 Class 方法来声明一个 Controller,实例如下:

  1. local IndexController = Class('controllers.index')
  2. -- curl http://localhost:9110/index/action_b
  3. function IndexController:action_b()
  4. return 'index->action_b'
  5. end
  6. return IndexController

这种情况下,可以定义 Controller 的构造器来对其进行初始化。示例如下:

  1. local IndexController = Class('controllers.index')
  2. function IndexController:__construct()
  3. self.aa = aa({info='ppppp'})
  4. end
  5. -- curl http://localhost:9110/index/action_b
  6. function IndexController:action_b()
  7. return 'index->action_b'
  8. end
  9. return IndexController

甚至还可以声明一个 Controller 基类,处理某些通用的逻辑,相关的详细用法参见 Vanilla面向对象 相关章节。

关于 Action 的返回值

Vanilla 底层会将 Action 执行的结果,完全使用 ngx.print 进行输出,所以 Action 的返回值必须不能为空。而由于 Vanilla 的 Response 中,提供了给响应添加头尾的 Response:appendBodyResponse:prependBody 方法,最终的结果会将这些部分合起来一起返回,所以 Action 的返回值要求如下:

  • Action 返回值必须非空
  • Action 返回值可以为一维索引数组(不可以是多维 Hash 数组)或者字符串