初识luci的cbi模块

本文介绍了如何使用luci的CBI(Config Base Interface)模块映射UCI配置文件,创建配置页面。通过示例代码展示了如何生成section、option,包括不同类型的控件如ListValue、Flag、Value等,并提供了应用配置的逻辑。同时,提到了相关参考资源,帮助读者深入理解luci的配置界面构建。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

流程为以下:

1、映射UCI文件

2、生成section

3、生成option

原config文件如下:

config login
    option username ''
    option password ''
    option ifname 'eth0'
    option domain ''

直接上代码:

require("luci.sys")

--[[

Map("配置文件文件名", "配置页面标题", "配置页面说明"),对应到配置文件/etc/config/testclient

]]--
m = Map("testclient", "the title of testclient", "the shuoming of testclient")

--[[

获取所有类型为loginsection并生成html section
class可以是TypedSection表示根据类型获取section
NamedSection
表示根据名字获取section
]]--

s = m:section(TypedSection, "login", "")
s.addremove = false
s.anonymous = true
--[[

生成option

class可以是:

Valueinput控件

ListValue:下拉列表
Flag
选择框
MultiValue:
DummyValue
:纯文本
TextValue
:多行input
Button
:按钮
StaticList

DynamicList:

下代码为每个section生成可选项控件,映射到proto字段
p= s:option(ListValue, “proto”, “Protocol”)
p:value(“static”, “static”)
p:value(“dhcp”, “DHCP”)
p.default = “static”

]]--

enable = s:option(Flag, "enable", translate("Enable"))
name = s:option(Value, "username", translate("Username"))
pass = s:option(Value, "password", translate("Password"))
pass.password = true
domain = s:option(Value, "domain", translate("Domain"))

ifname = s:option(ListValue, "ifname", translate("Interfaces"))
for k, v in ipairs(luci.sys.net.devices()) do
    if v ~= "lo" then
        ifname:value(v)
    end
end
local apply = luci.http.formvalue("cbi.apply")
if apply then
    io.popen("/etc/init.d/njitclient restart")
end

return m


控件对应的html文件在luasrc\view\cbi目录下



参考文章:

http://www.sharegogo.com/?p=568

http://www.cnblogs.com/mayswind/p/3468124.html#para2

http://luci.subsignal.org/trac/wiki/Documentation/CBI

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值