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

OpenWRT添加自定义LUCI页面示例

2020-01-15 08:28 3011 查看

1.文档结构

在openwrt源代码目录 /openwrt/feeds/luci/applications/下创建文件夹luci-myapplication。文件夹必须以luci-开头以便make menuconfig识别。按如下目录创建文件夹及文件。

[code]luci-myapplication
|---luasrc
|   |---controller
|   |   |---new_tab.lua
|   |---model
|   |   |---cbi
|   |       |---cbi_tab.lua
|   |---view
|       |---view_tab.htn
|---Makefile
|---root
|---etc
|---config
|---cbi_file

2.文件内容

2.1 luci-myapplication/luasrc/controller/new_tab.lua

[code]module("luci.controller.myapp.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", 60).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("cbi_tab"), "CBI Tab", 1)  --this adds the first sub-tab that is located in <luci-path>/luci-myapplication/model/cbi and the file is called cbi_tab.lua, also set to first position
entry({"admin", "new_tab", "tab_from_view"}, template("view_tab"), "View Tab", 2)  --this adds the second sub-tab that is located in <luci-path>/luci-myapplication/view and the file is called view_tab.htm, also set to the second position
end

2.2 luci-myapplication/luasrc/model/cbi/cbi_tab.lua

[code]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

2.3 luci-myapplication/root/etc/config/cbi_file

[code]config 'info' 'A'
option 'name' 'OpenWRT'

2.4 luci-myapplication/luasrc/view/view_tab.lua

[code]<%+header%>
<h1><%:Hello World%></h1>
<%+footer%>

2.5 luci-myapplication/Makefile

[code]include $(TOPDIR)/rules.mk

LUCI_TITLE:=LuCI Support for Test
LUCI_DEPENDS:=

include ../../luci.mk

# call BuildPackage - OpenWrt buildroot signature

3 编译过程

首先在openwrt源代码根目录下运行以下两个命令

[code]./scripts/feeds update luci
./scripts/feeds install -a -p luci

然后运行make menuconfig,选择LuCI --->3. applications --->luci-myapplication,保存配置,编译。

4.测试

将版本下载到设备并重启后,连接电脑和设备,电脑浏览器打开192.168.1.1,输入用户名&密码,进入如下界面

点击CBI_Tab

点击VIEW_Tab

5.遇到的问题

最开始编译完并下载到设备后,连接192.168.1.1能打开首页,但是点开CBI_Tab按钮时,页面加载出现错误,如下所示。

[code]Failed to execute cbi dispatcher target for entry '/admin/new_tab/tab_from_cbi'.
The called action terminated with an exception:
/usr/lib/lua/luci/dispatcher.lua:938: module 'luci.cbi' not found:
no field package.preload['luci.cbi']
no file './luci/cbi.lua'
no file '/usr/share/lua/luci/cbi.lua'
no file '/usr/share/lua/luci/cbi/init.lua'
no file '/usr/lib/lua/luci/cbi.lua'
no file '/usr/lib/lua/luci/cbi/init.lua'
no file './luci/cbi.so'
no file '/usr/lib/lua/luci/cbi.so'
no file '/usr/lib/lua/loadall.so'
no file './luci.so'
no file '/usr/lib/lua/luci.so'
no file '/usr/lib/lua/loadall.so'
stack traceback:
[C]: in function 'require'
/usr/lib/lua/luci/dispatcher.lua:938: in function </usr/lib/lua/luci/dispatcher.lua:937>

解决办法1.确保openwrt设备联网,然后输入以下命令:

[code]root@OpenWrt:/# opkg update
root@OpenWrt:/# opkg install luci-compat
root@OpenWrt:/#

解决办法2.在源代码中将luci-compat编译进去。在源代码根目录输入make menuconfig,然后选择LuCI--->2.Modules --->luci-compat,如下图所示,保存更改,重新编译并下载到设备上即可。

6.参考网址

https://openwrt.org/docs/guide-developer/luci

https://www.geek-share.com/detail/2697725715.html

  • 点赞
  • 收藏
  • 分享
  • 文章举报
ultra seven 发布了9 篇原创文章 · 获赞 8 · 访问量 3034 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: