您的位置:首页 > 运维架构

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

2015-09-28 15:51 1656 查看

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

本篇博客主要描述luci添加菜单的两个实例,即CBI和View(Template):

添加新元素到luci中去

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

添加cbi标签的代码

添加cbi配置文件

添加view标签代码

关键字

luci

cbi

view

template

fulinux

添加新元素到luci中去

这里将向大家展示如何在luci中添加新标签的方法。
作为一个实例我将向大家展示luci添加新标签的两种方法:

CBI

2.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 <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%>


效果图展示

CBI:



VIEW:



声明

作者:fulinux

地址:点击fulinux博客

版权:可以自由转载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: