方式一:
主要描述luci直接添加新标签的两种方法:
<1>.CBI
<2>.View(template)
添加新的顶级选项卡标签(主菜单)
添加cbi标签的代码
添加cbi配置文件
添加view标签代码
关键字:luci cbi view template fulinux
1.添加新的顶级选项卡标签(主菜单)
在浏览器地址栏上通过输入192.168.1.1(我的是192.168.1.91)地址即可访问openwrt的web界面,主菜单包括Status,System,Network和logout。
这里我们要加入一个新的主菜单名为:”New Tab1”
登录openwrt后在/usr/lib/lua/luci/controller/admin目录下添加new_tab.lua文件,文件内容如下:
root@OpenWrt:/# vim /usr/lib/lua/luci/controller/admin/
filebrowser.lua network.lua status.lua test.lua
index.lua servicectl.lua system.lua uci.lua
root@OpenWrt:/# vim /usr/lib/lua/luci/controller/admin/new_tab1.lua
root@OpenWrt:/# cat /usr/lib/lua/luci/controller/admin/new_tab1.lua //module("luci.controller.admin.new_tab",package.seeall())
function index()
entry({"admin","new_tab"},firstchild(),"New tab1",30).dependent=false
entry({"admin","new_tab","tab_from_cbi"},cbi("admin_myapp/cbi_tab"),"CBI Tab",1)
entry({"admin","new_tab","tab_from_view"},template("admin_myapp/view_tab"),"VIEW Tab",2)
end
module("luci.controller.admin.new_tab",package.seeall())
function index()
entry({"admin","new_tab"},firstchild(),"New tab1",30).dependent=false
entry({"admin","new_tab","tab_from_cbi"},cbi("admin_myapp/cbi_tab"),"CBI Tab",1)
entry({"admin","new_tab","tab_from_view"},template("admin_myapp/view_tab"),"VIEW Tab",2)
end
2.添加cbi标签的代码
按照上面new_tab.lua文件中的代码,我们需要在/usr/lib/lua/luci/model/cbi/admin_myapp目录下新建一个cbi_tab.lua文件,包含如下代码:
root@OpenWrt:/# mkdir /usr/lib/lua/luci/model/cbi/admin_myapp
root@OpenWrt:/# cd /usr/lib/lua/luci/model/cbi/admin_myapp
root@OpenWrt:/usr/lib/lua/luci/model/cbi/admin_myapp# vim cbi_tab.lua
root@OpenWrt:/usr/lib/lua/luci/model/cbi/admin_myapp# cat 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
3.添加cbi配置文件
从上面的代码我们知道需要一个config文件包含section和options,在这里我们在/etc/confi目录下新建一个cbi_file文件,类似如下内容:
root@OpenWrt:~# vim /etc/config/cbi_file
root@OpenWrt:~# cat /etc/config/cbi_file
config 'info' 'A'
option 'name' 'OpenWRT'
4.添加view标签代码
最后我们在/usr/lib/lua/luci/view/admin_myapp目录下新建view_tab.htm文件,包含如下代码:
root@OpenWrt:~# mkdir /usr/lib/lua/luci/view/admin_myapp
root@OpenWrt:~# cd /usr/lib/lua/luci/view/admin_myapp
root@OpenWrt:/usr/lib/lua/luci/view/admin_myapp# vim view_tab.htm
root@OpenWrt:/usr/lib/lua/luci/view/admin_myapp# cat view_tab.htm<%+header%>
<h1><%:Hello World%></h1>
<%+footer%>
//说明:重启openwrt是可以看到的!
方式二:
我们可以在源码上进行添加,然后再进行编译移植内核。
1.进入openwrt/feeds/luci/application,添加如下目录结构:
[root@crmn luci-myapplication]# pwd
/root/tau_svn/tau/oolite/openwrt-15.05-release/feeds/luci/applications/luci-myapplication
[root@crmn luci-myapplication]# tree
.
├── luasrc
│ ├── controller
│ │ └── myapp
│ │ └── new_tab.lua
│ ├── model
│ │ └── cbi
│ │ └── myapp-mymodule
│ └── view
│ └── myapp-mymodule
└── Makefile
8 directories, 2 files
2.在luci-myapplication目录下新建一个Makefile,内容如下:
[root@crmn luci-myapplication]# cat Makefile
include $(TOPDIR)/rules.mk
LUCI_TITLE:=LuCI Support for Test
LUCI_DEPENDS:=include ../../luci.mk
#call BuildPackage - OpenWrt buildroot signature
3.在myapp目录下新建new_tab.lua,内容如下:
[root@crmn luci-myapplication]# cat luasrc/controller/myapp/new_tab.lua
module("luci.controller.myapp.new_tab",package.seeall) --new_tab要与文件名一致
function index()
entry({"admin","new_tab"},firstchild(),"New tab",60).dependent=false --添加顶层导航
entry({"admin","new_tab","tab_from_cbi"},cbi("myapp-mymodule/cbi_tab"),"CBI Tab",1)--在New tab下添加一个子选项CBI Tab
entry({"admin","new_tab","tab_from_view"},template("myapp-mymodule/view_tab"),"View Tab",2)
end
4.在cbi/myapp-mymodule目录下新建cbi_tab.lua,内容如下:
[root@crmn luci-myapplication]# vim luasrc/model/cbi/myapp-mymodule/cbi_tab.lua
[root@crmn luci-myapplication]# cat luasrc/model/cbi/myapp-mymodule/cbi_tab.luam=Map("cbi_file",translate("First Tab Form"),translate("please fill out the form below"))--cbi_file is the config file in /etc/config
//template()是因为字符串中有空格,若没有空格就不用该函数
d=m:section(TypeSection,"info","Part A of the form")--info is the section called info in cbi_file
//修改'info'类型选项
a=d:option(Value,"name","Name");//创建一个标准的文本框,该文本框中的值会对应修改info类型
a.optional=false;//该选项不是可选项
a.rmempty=false;--name is the option in the cbi_file//当用户输入一个空值时从配置文件中移除该选项
return m
//说明:CBI去修改UCI配置文件的值,CBI是通过web控件选择值去修改UCI文件。,所有的CBI文件都必须返回一个luci.cbi.Map类型的对象。
myapp-mymodule目录下新建view_tab.htm,内容如下:
[root@crmn luci-myapplication]# vim luasrc/view/myapp-mymodule/view_tab.htm
[root@crmn luci-myapplication]# cat luasrc/view/myapp-mymodule/view_tab.htm
<%+header%>
<h1><%:hello luci2,meng%></h1>
<%+footer%>
6.执行更新:
./scripts/feeds update luci
./scripts/feeds install -a -p luci
make menuconfig
[root@crmn openwrt-15.05-release]# ./scripts/feeds update luci
Updating feed 'luci' from 'https://github.com/openwrt/luci.git;for-15.05' ...
remote: Counting objects: 400, done.
remote: Compressing objects: 100% (132/132), done.
remote: Total 400 (delta 184), reused 373 (delta 159), pack-reused 0
Receiving objects: 100% (400/400), 83.45 KiB | 71 KiB/s, done.
Resolving deltas: 100% (184/184), completed with 121 local objects.
From https://github.com/openwrt/luci
07e7ff0..a9bddc4 lede-17.01 -> origin/lede-17.01
87ec343..c647121 master -> origin/master
Already up-to-date.
Create index file './feeds/luci.index'
Collecting package info: done
[root@crmn openwrt-15.05-release]# ./scripts/feeds install -a -p luci
Installing all packages from feed luci.
Installing package 'luci-myapplication'[root@crmn openwrt-15.05-release]# make menuconfig
LuCI --->
3. Applications --->
<*> luci-myapplication................................. LuCI Support for Test
[root@crmn openwrt-15.05-release]# make V=99^C
[root@crmn openwrt-15.05-release]# ls bin/ramips/
md5sums
openwrt-ramips-mt7621-dir-860l-b1-squashfs-factory.bin
openwrt-ramips-mt7621-dir-860l-b1-squashfs-sysupgrade.bin
openwrt-ramips-mt7621-firewrt-squashfs-sysupgrade.bin
openwrt-ramips-mt7621-mt7621-squashfs-sysupgrade.bin
openwrt-ramips-mt7621-oolite-v8-16MB-15.05-squashfs-sysupgrade.bin
openwrt-ramips-mt7621-oolite-v8-32MB-15.05-squashfs-sysupgrade.bin
openwrt-ramips-mt7621-oolite-v8-8MB-15.05-squashfs-sysupgrade.bin
openwrt-ramips-mt7621-pbr-m1-squashfs-sysupgrade.bin
openwrt-ramips-mt7621-re6500-squashfs-sysupgrade.bin
openwrt-ramips-mt7621-wsr-1166-squashfs-sysupgrade.bin
openwrt-ramips-mt7621-wsr-600-squashfs-sysupgrade.bin
openwrt-ramips-mt7621-zbt-wg2626-squashfs-sysupgrade.bin
packages
sha256sums
7.编译,烧录移植内核
8.在开发板/etc/config目录下新建cbi_file文件,内容如下:
root@OpenWrt:/# vim etc/config/
dhcp dropbear firewall luci network rpcd system ucitrack uhttpd
root@OpenWrt:/# vim etc/config/cbi_file
root@OpenWrt:/# cat etc/config/cbi_file
config 'info' 'A'
option 'name' 'OpenWRTcrmn'
9.在浏览器输入192.168.1.1进入web界面,效果如下: