Openwrt系统中luci学习

1.Luci采用的是MCV框架,即是model,controller,view.在烧录了编译了luci的openwrt系统中,可以找到lua/luci陌路下的MCV三个目录.其中model是抽象层,可以通过cbi开发基于框架下的界面;controller是控制层;而view则是界面层用来存放htm文件,自己设计的界面.

2.在开发中有几种模式,一是修改原有的系统界面,这个我没有修改,因为原有的已经很perfect了

二是单独的创建新的cbi文件开发新的web页面,例子:

在controller/admin目录下创建一个test.lua文件

module("luci.controller.admin.test", package.seeall)

function index()
    entry({"admin", "test"}, alias("admin", "test", "test"), _("TEST"), 30).index = true
    entry({"admin", "test", "test"}, cbi("admin_test/test"), _("test"), 1)
end
~

 在model/cbi/下创建文件夹admin_test,并在文件夹下放test.lua文件

-- Copyright 2008 Steven Barth <[email protected]>
-- Copyright 2008 Jo-Philipp Wich <[email protected]>
-- Licensed to the public under the Apache License 2.0.0

f = SimpleForm("Test", translate("test"), translate("This is a test!!!"))
--将框架提供的reset和submit按钮去掉
f.reset = false
f.submit = false

--软件延时n秒
function sleep(n)
        local t0 = os.clock()
        while os.clock()-t0 <= n do
        end
end

test = {{year="2018-10-22",time="10:10",week="周一"}}
t = f:section(Table,test)
t:option(DummyValue, "year", translate("Year"))
t:option(DummyValue, "time", translate("Time"))
t:option(DummyValue, "week", translate("Week"))

scan = t:option(Button, "_remove", translate("remove"))
--inputstyle:remove,reload,reset
scan.inputstyle = "apply"
function scan.write(self, section)
        luci.sys.exec("rm -rf /tmp/luci*")

end

return f

三是通过创建view下的htm文件实现界面交互,以输入路径删除文件为例子:

创建controller/admin/devConfig.lua

module("luci.controller.admin.devConfig", package.seeall)
local fs  = require "nixio.fs"

function index()
    entry({"admin", "devConfig"}, alias("admin", "devConfig", "devConfig"), _("Device Configure"), 30).index = true
    --回调函数
    entry({"admin", "devConfig", "devConfig"}, call("action_devConfig"), _("DeviceScan"), 1)
    
end

function action_devConfig()
         --加载htm文件
         luci.template.render("admin_devConfig/devConfig", {})
         --读取输入框的内容
         local name = luci.http.formvalue("name")
         if name then
                if luci.http.formvalue("delete") then
                        local str = string.format("rm -rf %s",name)
                        if fs.access(name) then
                                luci.sys.exec(str)
                        end
                end
         end
end

创建view/admin_devConfig/devConfig.htm

<%#
 Copyright 2008 Steven Barth <[email protected]>
 Copyright 2008 Jo-Philipp Wich <[email protected]>
 Licensed to the public under the Apache License 2.0.1
-%>

<%+header%>

<h2><a id="content" name="content"><%:Scan Device%></a></h2>

<ul class="cbi-tabmenu">
        <li class="cbi-tab"><a href="#"><%:Actions%></a></li>
</ul>

<fieldset class="cbi-section">
        <fieldset class="cbi-section">
                <legend><%:Delete%></legend>
                <form method="post" action="<%=REQUEST_URI%>" enctype="multipart/form-data">
                        <div class="cbi-section-node">
                                <div class="cbi-value-field">
                                        <label class="cbi-value-title" for="archive"><%:deletename%>:</label>
                                        --name字段很重要,用于控制模块调用访问的
                                        <input type="text" name="name" id="name" />
                                        <input type="submit" class="cbi-button cbi-input-apply" name="delete" value="<%:Delete%>" />
                                </div>
                        </div>
                </form>
        </fieldset>

</fieldset>
<%+footer%>

四是cbi与htm的混合使用,下期再学习吧

猜你喜欢

转载自blog.csdn.net/gg101001/article/details/83268675