赞
踩
openwrt 官方文档:
Create a sample procd init script
https://openwrt.org/docs/guide-developer/procd-init-script-example
luci官方github:ModulesHowTo
https://github.com/openwrt/luci/wiki/ModulesHowTo
API
http://openwrt.github.io/luci/api/index.htmlx
http://openwrt.github.io/luci/jsapi/LuCI.html
第三方总结:
⭐️Openwrt:LuCI入门(一)
https://blog.csdn.net/qq_28812525/article/details/103870169
⭐️Openwrt:LuCI之CBI(二)
https://blog.csdn.net/qq_28812525/article/details/103881723
⭐️Openwrt:LuCI之UCI(三)
https://blog.csdn.net/qq_28812525/article/details/103902872
OpenWrt的Luci之Web页面开发
https://blog.csdn.net/qq_37035946/article/details/93748061
OpenWRT - WEB界面开发思路和基本方法
https://www.cnblogs.com/h2zZhou/p/10357311.html
⭐️OpenWRT 专题 十多篇
https://blog.csdn.net/u012041204/category_6585005.html
MVC模型,将涉及的三个文件夹列出来:
/usr/lib/lua/luci/controller/*
/usr/lib/lua/luci/view/*
/usr/lib/lua/luci/model/cbi/*
后面我们也将围绕这三个文件夹进行界面开发。
root@OpenWrt_D2550:~# cat /usr/lib/lua//luci/view/myapp-mymodule/helloworld.htm
<%+header%>
<h1>Hello World</h1>
<%+footer%>
配合下面的controller可以实现下面效果
语法说明
<% code %>
<% write(value) %>
or <%=value%>
<% include(templatesName) %>
<%+templatesName%>
<%= translate(“Text to translate”) %>
<%:Text to translate%>
<%# comment %>
其他语法跟html和JavaScript一样。
mkdir /usr/lib/lua/luci/controller/myapp
vim /usr/lib/lua/luci/controller/myapp/new_tab.lua
在文件中输入如下内容
module("luci.controller.myapp.new_tab", package.seeall)
function index()
entry({"admin", "new_tab"}, firstchild(), translate("cfg"), 1).dependent=false
entry({"admin", "new_tab", "sn"}, cbi("myapp-mymodule/gateway_sn"), translate("sn"), 2)
entry({"admin", "new_tab", "hellworld"}, template("myapp-mymodule/helloworld"), _("HelloWorld"), 3)
end
api解释
module(“luci.controller.myapp.new_tab”, package.seeall)
定义模块的入口
entry(path, target, title=nil, order=nil)
entry表示添加一个新的选项入口
Action
)、访问指定页面(Views
)以及调用CBI Module
。
call(“function_name”)
”,然后在该lua文件下编写名为function_name的函数就可以调用了。template(“myapp-mymodule/helloworld”)
”就可以调用/usr/lib/lua/luci/view/myapp-mymodule/helloworld.htm文件了。cbi(“myapp/mymodule”)
”就可以调用/usr/lib/lua/luci/model/cbi/myapp-mymodule/gateway_sn.lua文件了translate(“英文/中文”)
,也可以用_(“HelloWorld”)
方式,还有一种就是利用.po
文件,将英文标识 与 翻译语言 一一对应,例如:msgid “Default gateway”
msgstr “默认网关”
_("Default gateway")
时,如果路由设置为中文显示,则该选项自动显示为 默认网关。mkdir /usr/lib/lua/luci/model/cbi/myapp-mymodule
vim /usr/lib/lua/luci/model/cbi/myapp-mymodule/gateway_sn.lua
文件中输入如下内容:
m = Map("sn_file", translate("产品序列号")) -- cbi_file is the config file in /etc/config
d = m:section(TypedSection, "gateway_sn") -- info is the section called info in cbi_file
a = d:option(Value, "sn", translate("序列号"));
a.optional=false;
a.rmempty = false; -- name is the option in the cbi_file
return m
语法说明:
官方文档 http://luci.subsignal.org/trac/wiki/Documentation/CBI
或详情 见 Openwrt:LuCI之CBI(二)
https://blog.csdn.net/qq_28812525/article/details/103881723
vim /etc/config/sn_file
config 'gateway_sn' 'sn'
option 'sn' 'gw2019081300001'
语法说明:
详情 见 Openwrt:LuCI之UCI(三)
https://blog.csdn.net/qq_28812525/article/details/103902872
创建目录及文件
进入openwrt源码/feeds/luci/applications/,添加如下目录文件结构
lawsssscat@lawsssscat-virtual-machine:~/lede/feeds/luci/applications$ tree luci-app-myapplication/
luci-app-myapplication/
└── luasrc
├── controller
│ └── myapp
├── model
│ └── cbi
│ └── myapp-mymoeule
└── view
└── myapp-mymodule
vim luci-app-myapplication/Makefile
include $(TOPDIR)/rules.mk
LUCI_TITLE:=LuCI Support for Test
LUCI_DEPENDS:=
include ../../luci.mk
# call BuildPackage - OpenWrt buildroot signature
在luci-app-myapplication/luasrc/controller/myapp/new_tab.lua
文件中 ,添加内容如下(注:与上面系统添加界面方式的内容一致):
module("luci.controller.myapp.new_tab", package.seeall)
function index()
entry({"admin", "new_tab"}, firstchild(),translate("cfg"), 1).dependent=false
entry({"admin", "new_tab", "sn"}, cbi("myapp-mymodule/gateway_sn"), translate("sn"), 2)
entry({"admin", "new_tab", "hellworld"}, template("myapp-mymodule/helloworld"), _("HelloWorld"), 3)
end
在luci-app-myapplication/luasrc/model/cbi/myapp-mymodule/gateway_sn.lua文件中 ,添加内容如下(注:与上面系统添加界面方式的内容一致):
m = Map("sn_file", translate("产品序列号")) -- cbi_file is the config file in /etc/config
d = m:section(TypedSection, "gateway_sn") -- info is the section called info in cbi_file
a = d:option(Value, "sn", translate("序列号"));
a.optional=false;
a.rmempty = false; -- name is the option in the cbi_file
return m
在luci-app-myapplication/luasrc/view/myapp-mymodule/helloworld.htm 文件中 ,添加内容如下(注:与上面系统添加界面方式的内容一致):
<%+header%>
<h1><%: HelloWorld %></h1>
<%+footer%>
在openwrt源码目录 openwrt源码/package/base-files/files/etc/config/ 新建cbi_file文件
config 'gateway_sn' 'sn'
option 'sn' 'gw2019081300001'
回退到Openwrt 源码顶层目录,依次执行以下命令:
./scripts/feeds update luci
./scripts/feeds install -a -p luci
make menuconfig
进入
LuCI --->
Applications --->
<*> luci-app-myapplication............................. LuCI Support for Test
选定,然后保存退出,编译 make V=s
烧录固件
注:凡是修改controller/文件夹中的配置,都需要重启板子或把/tmp/目录下luci-indexcache luci-modulecache/luci-sessions/删除 才能生效,其他几个文件夹修改可不用,刷新一下网页即可
root@openwrt_d2550:~# cat /etc/config/luci
config core 'main'
option resourcebase '/luci-static/resources'
option lang 'zh_cn'
option mediaurlbase '/luci-static/bootstrap'
config extern 'flash_keep'
option uci '/etc/config/'
option dropbear '/etc/dropbear/'
option openvpn '/etc/openvpn/'
option passwd '/etc/passwd'
option opkg '/etc/opkg.conf'
option firewall '/etc/firewall.user'
option uploads '/lib/uci/upload/'
config internal 'languages'
option zh_cn '简体中文 (Simplified Chinese)'
config internal 'sauth'
option sessionpath '/tmp/luci-sessions'
option sessiontime '3600'
config internal 'ccache'
option enable '1'
config internal 'themes'
option Argon '/luci-static/argon'
option ATMaterial '/luci-static/atmaterial'
option ATMaterial_red '/luci-static/atmaterial_red'
option ATMaterial_Brown '/luci-static/atmaterial_Brown'
option Bootstrap '/luci-static/bootstrap'
option Edge '/luci-static/edge'
option Material '/luci-static/material'
option Neobird '/luci-static/neobird'
option Netgear '/luci-static/netgear'
option tomato '/luci-static/tomato'
option Argonne '/luci-static/argonne'
option ifit '/luci-static/ifit'
option mcat '/luci-static/mcat'
config internal 'diag'
option dns 'openwrt.org'
option ping 'openwrt.org'
option route 'openwrt.org'
路径错了
No page is registered at '/myt'.
If this url belongs to an extension, make sure it is properly installed.
If the extension was recently installed, try removing the /tmp/luci-indexcache file.
/usr/lib/lua/luci/dispatcher.lua:350: Access Violation
The page at 'mytest/sethostname/' has no parent node so the access to this location has been denied.
This is a software bug, please report this message at https://github.com/openwrt/luci/issues
stack traceback:
[C]: in function 'assert'
/usr/lib/lua/luci/dispatcher.lua:350: in function 'dispatch'
/usr/lib/lua/luci/dispatcher.lua:126: in function </usr/lib/lua/luci/dispatcher.lua:125>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。