網頁

2017年3月22日 星期三

luci 之 MVC 模組

參考 寫個 LuCI 模組

control 模組
root@arduino:/# cd /usr/lib/lua/luci/controller/
root@arduino:/usr/lib/lua/luci/controller# mkdir myapp
root@arduino:/usr/lib/lua/luci/controller# cd myapp
root@arduino:/usr/lib/lua/luci/controller/myapp# vi mymodule.lua
-- Save this to /usr/lib/lua/luci/controller/myapp/mymodule.lua
-- Browse to: /cgi-bin/luci/myapp/mymodule/time.htm

module("luci.controller.myapp.mymodule", package.seeall)

function index()
  -- 定義節點, request 時會呼叫 action_time
  page = entry({"myapp", "mymodule", "time.htm"}, call("action_time"))

  -- 沒有上層節點
  page.dependent = false
  -- 假如是葉節點, 就設成 true 避免錯誤
  page.leaf = true
end

function action_time()
  luci.http.prepare_content("text/html")
  luci.http.write("<h1>Hello LuCi</h1>")
  luci.http.write("<h2>Current time: " .. os.date("%D %T") .. "</h2>")
end

http://arduino.local/cgi-bin/luci/myapp/mymodule/time.htm

control - view 模組
root@arduino:/usr/lib/lua/luci/controller/myapp# cd /usr/lib/lua/luci/view/
root@arduino:/usr/lib/lua/luci/view# mkdir myapp_mymodule
root@arduino:/usr/lib/lua/luci/view# cd myapp_mymodule/
root@arduino:/usr/lib/lua/luci/view/myapp_mymodule# vi time.htm
<!-- Save this to /usr/lib/lua/luci/view/myapp_mymodule/time.htm -->
<%+header%>
<h1>Hello LuCI</h1>
<h2>Current Time: <%= os.date("%D %T")%></h2>
<%+footer%>

root@arduino:/usr/lib/lua/luci/view/myapp_mymodule# cd /usr/lib/lua/luci/controller/myapp/
root@arduino:/usr/lib/lua/luci/controller/myapp# vi mymodule2.lua
-- Save this to /usr/lib/lua/luci/controller/myapp/mymodule2.lua
-- Browse to: /cgi-bin/luci/myapp/mymodule/
-- Browse to: /cgi-bin/luci/myapp/mymodule/time.html

module("luci.controller.myapp.mymodule2", package.seeall)

function index()
  -- 定義節點, 當作 myapp.mymodule.time.html 的別名
  page = entry({"myapp", "mymodule"},
               alias("myapp", "mymodule", "time.html"), "My Module")
  page.dependent = false

  -- 定義節點, request 時會 render view/myapp_mymodule/time.html
  page = entry({"myapp", "mymodule", "time.html"},
               template("myapp_mymodule/time"), "Time")
  page.dependent = false; page.leaf = true
end

http://arduino.local/cgi-bin/luci/myapp/mymodule
http://arduino.local/cgi-bin/luci/myapp/mymodule/time.html



沒有留言:

張貼留言