OpenWrt的luci web管理器添加新菜单

添加新的顶级选项卡标签(主菜单)

我们在浏览器地址栏上通过输入192.168.1.1我的是(192.168.0.108)地址即可访问openwrt的web界面,主菜单包括Status,System,Network和logout,如图所示:

这里我们要加入一个新的主菜单名为:”New Tab”

登录openwrt后在/usr/lib/lua/luci/controller/admin目录下添加new_tab.lua文件,文件内容如下:

-- Copyright 2008 fulinux <fulinux@sina.com>
-- 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

添加cbi标签的代码

按照上面new_tab.lua文件中的代码,我们需要在/usr/lib/lua/luci/model/cbi/admin_myapp目录下新建一个cbi_tab.lua文件,包含如下代码:

-- Copyright 2008 fulinux <fulinux@sina.com>
-- 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

添加cbi配置文件

从上面的代码我们知道需要一个config文件包含section和options,在这里我们在/etc/confi目录下新建一个cbi_file文件,类似如下内容:

config 'info' 'A'
    option 'name' 'OpenWRT'

添加view标签代码

最后我们在/usr/lib/lua/luci/view/admin_myapp目录下新建view_tab.htm文件,包含如下代码:

<%+header%>                                                                    
<h1><%:Hello World%></h1>                                                      
<%+footer%>

转自:https://blog.csdn.net/fulinus/article/details/48785449

原文地址:https://www.cnblogs.com/biao-wu/p/13093437.html