添加新的頂級選項卡標簽(主菜單)
我們在瀏覽器地址欄上通過輸入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%>