本篇博客主要描述luci添加菜单的两个实例,即CBI和View(Template):
我们在浏览器地址栏上通过输入192.168.1.1(我的是192.168.170.1)地址即可访问openwrt的web界面,主菜单包括Status,System,Network和logout,如图所示:
这里我们要加入一个新的主菜单名为:”New Tab”
登录openwrt后在/usr/lib/lua/luci/controller/admin目录下添加new_tab.lua文件,文件内容如下:
-- Copyright 2008 fulinux
-- Licensed to the public under the Apache License 2.0.
module("luci.controller.admin.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", 30).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("admin_myapp/cbi_tab"), "CBI Tab", 1) --this adds the first sub-tab that is located in /usr/lib/lua/luci/model/cbi/admin_myapp and the file is called cbi_tab.lua, also set to first position
entry({"admin", "new_tab", "tab_from_view"}, template("admin_myapp/view_tab"), "View Tab", 2) --this adds the second sub-tab that is located in /usr/lib/lua/luci/view/admin_myapp and the file is called view_tab.htm, also set to the second position
end
按照上面new_tab.lua文件中的代码,我们需要在/usr/lib/lua/luci/model/cbi/admin_myapp目录下新建一个cbi_tab.lua文件,包含如下代码:
-- Copyright 2008 fulinux
-- Licensed to the public under the Apache License 2.0.
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
从上面的代码我们知道需要一个config文件包含section和options,在这里我们在/etc/confi目录下新建一个cbi_file文件,类似如下内容:
config 'info' 'A'
option 'name' 'OpenWRT'
最后我们在/usr/lib/lua/luci/view/admin_myapp目录下新建view_tab.htm文件,包含如下代码:
<%+header%>
<h1><%:Hello World%>h1>
<%+footer%>
作者:fulinux
地址:点击fulinux博客
版权:可以自由转载