openwrt lua mysql_初识Lua——OpenWrt路由界面配置

本文介绍了OpenWrt路由界面配置中LuCI系统的目录结构和功能,特别是Lua中的`entry`函数用于模块注册,以及`call`、`template`和`cbi`三种目标类型。重点讲解了`cbi`模块在界面元素组合中的作用,通过实例展示了如何创建和响应Button控件。最后,作者表示期待与技术大神交流,共同学习进步。
摘要由CSDN通过智能技术生成

OpenWrt路由的界面配置使用LuCI系统管理。

4053467bf4bce260f33a6578da0f0ebe.png

在此,对其中的目录结构进行介绍:

-目录结构

以status模块为例进行说明,模块入口文件status.lua在目录lua\luci\controller\admin下。

functionindex()

entry({"admin", "status"},alias("admin", "status", "overview"),_("Status"), 20).index= true

entry({"admin", "status", "overview"},template("admin_status/index"),_("Overview"), 1)

entry({"admin", "status", "iptables"}, call("action_iptables"),_("Firewall"), 2).leaf= true

……

entry({"admin", "status", "processes"},cbi("admin_status/processes"),_("Processes"), 6)

……

end

在index()函数中,使用entry函数来完成每个模块函数的注册,官方说明文档如下:

entry(path, target, title=nil, order=nil)

path is a table that describes the position in the dispatching tree: For example a path of {"foo", "bar", "baz"} would insert your node in foo.bar.baz.

target describes the action that will be taken when a user requests the node. There are several predefined ones of which the 3 most important (call, template, cbi) are described later on on this page

title defines the title that will be visible to the user in the menu (optional)

order is a number with which nodes on the same level will be sorted in the menu (optional)

其中target主要分为三类:call,template和cbi。

call用来调用函数。

即语句entry({"admin", "status", "iptables"}, call("action_iptables"),_("Firewall"), 2)

Firewall模块调用了action_iptables函数:

functionaction_iptables()

ifluci.http.formvalue("zero") then

……

end

end

template用来调用已有的htm模版,模版目录在lua\luci\view目录下。

即语句entry({"admin", "status", "overview"},template("admin_status/index"),_("Overview"), 1)

调用了lua\luci\view\admin_status\index.htm文件来显示。

cbi语句使用cbi模块,这是使用非常频繁也非常方便的模块,在cbi模块中定义各种控件,Luci系统会自动执行大部分处理工作。其链接目录在lua\luci\model\cbi下。

显然语句entry({"admin", "status", "processes"},cbi("admin_status/processes"),_("Processes"), 6)

调用lua\luci\model\cbi\admin_status\processes.lua来实现模块。

这样我们可以发现,cbi模块可能是核心功能模块了,我们看看这个模块的使用。

-cbi模块

cbi模块包含的一系列lua文件构成界面元素的组合,所有cbi模块中的控件都需要写在luci.cbi.Map中。

在cbi.lua文件中封装了所有的控件元素,例如复选框,下拉列表等。

c82af52b39bd6da7e558d736870deca3.png

常用控件的具体说明可以参照LuCI Documentation中的描述。

在此简单地举例Button来说明其应用

button=s:option(Button, "_button", "Button")

button.inputtitle=translate("exec")

button.inputstyle= "apply"

functionbutton.write(self,section,value)

AbstractValue.write(self,section,value)

locallistvalue=luci.fs.readfile("/etc/saveValue")

os.execute("touch /etc/testfile%s" %{listvalue})

self.inputtitle=translate("haha")

end

按钮的响应过程为:从saveValue文件中获取内容,然后以获取到的字符串命名创建新文件。

截一张学习测试界面时的图:

d5989943f60941bb6d23e7c93b70a54f.png

Lua语言也是这几天才开始接触,了解必然是有局限性的,wayne欢迎大神们的指导,希望能共同促进!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值