这是一个展示如何向LuCI接口添加新元素的示例。
增加一个顶级页签
首先,我们将在顶部导航中添加一个新的标签。通常人们可以看到:Freifunk | Status | System | Services | Network等。
您可以通过在<luci-path>/applications/luci-myapplication/luasrc/controller/myapp中添加一个文件到控制器目录来实现这一点。
<luci-path>: LuCI源文件路径;
luci-myapplication: 为此例子创建的一个新目录. 这个目录的名字必须要以luci-开头,这样才能被make menuconfig识别为一个模块,并作为一个模块被编译;
controller:UI控制的默认目录;
myapp:为此例子增加的目录;
我们假设新增文件名字为: new_tab.lua,该文件内容如下:
module("luci.controller.myapp.new_tab", package.seeall) --notice that new_tab is the name of the file new_tab.lua,表示创建一个模块 function index() entry({"admin", "new_tab"}, firstchild(), "New tab", 60).dependent=false --this adds the top level tab and defaults to the first sub-tab (tab_from_cbi), also it is set to position 30 entry({"admin", "new_tab", "tab_from_cbi"}, cbi("myapp-mymodule/cbi_tab"), "CBI Tab", 1) --this adds the first sub-tab that is located in <luci-path>/luci-myapplication/model/cbi/myapp-mymodule and the file is called cbi_tab.lua, also set to first position entry({"admin", "new_tab", "tab_from_view"}, template("myapp-mymodule/view_tab"), "View Tab", 2) --this adds the second sub-tab that is located in <luci-path>/luci-myapplication/view/myapp-mymodule and the file is called view_tab.htm, also set to the second position end |
添加cbi_tab代码
根据上面的代码,我们需要创建一个cbi_tab.lua文件,路径:
<luci-path>/luci-myapplication/model/cbi/myapp-mymodule
该文件中的代码如下,该文件定义了一个配置文件、section和option
m = Map("cbi_file", translate("First Tab Form"), translate("Please fill out the form below")) -- cbi_file is the config file in /etc/config d = m:section(TypedSection, "info", "Part A of the form") -- info is the section called info in cbi_file a = d:option(Value, "name", "Name"); a.optional=false; a.rmempty = false; -- name is the option in the cbi_file return m |
添加cbi_file文件
从上面的代码中,我们知道我们需要一个具有适当的section和option的配置文件。在我们的例子中,我们将在/etc/配置中创建文件cbi_file,如下所示:
config 'info' 'A' option 'name' 'OpenWRT' |
添加view_tab代码
文件view_tab.htm 应该放在<luci-path>/applications/luci-myapplication/luasrc/view/myapp-mymodule/路径下,具体内容如下:
<%+header%> <h1><%:Hello World%></h1> <%+footer%> |
module函数和entry函数
module函数
将该文件生成一个模块,可以通过requeire(“model_name”)来载入该模块,文件的后缀名是.lua。
entry函数
entry 功能函数的原型为entry(path, target, title=nil, order=nil)
前面两个参数是必须的,后面两个是可选的。
Path 形如(admin,system,reboot),entry 根据这些属性创建节点,比如上面内容代表在 system 标签中的 reboot 页面,target 是这里的重点,它描述了当用户调用这个界面所执行的行为。这里主要由三种行为,call、template、cbi,
- call 为调用文件中的功能函数,
- template 为调用 view 中的 html 界面,
- cbi 为调用 mode 中的 lua 文件。
Title 是在页面中显示的内容,order 是显示的顺序,顺序越小显示内容越靠前。下面在 system.lua 中增加一条:
entry({"admin", "system", "helloworld"}, template("admin_system/helloworld"), _("helloworld "), 99)
- 第一个参数代表一个节点,
- 第二个参数,调用 view/admin_system/helloworld.htm 文件,
- 第三个参数为显示的名字,
- 第四个参数为显示顺序,这里在最后显示。