Chinaunix首页 | 论坛 | 博客
  • 博客访问: 722736
  • 博文数量: 77
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1173
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-16 11:20
个人简介

将技术做到极致...

文章分类

全部博文(77)

文章存档

2019年(3)

2015年(27)

2014年(47)

分类: LINUX

2015-05-26 16:04:55

一、引言
    OpenWRT中采用LuCI作为它的Web interface界面框架,采用Lua语言。在本文中将以一个简单的示例详细描述如何自定义开发一个界面,对一个配置文件进行操作。
二、Model与Controler 

    MVC的设计理念是进行LuCI开发的一个关键,什么是MVC请参看如下Blog:http://www.cnblogs.com/gnuhpc/archive/2012/12/21/2827597.html在LuCI中Controller的文件定义在固件中    的/usr/lib/lua/luci/controller目录中,模版目录在/usr/lib/lua/luci/view目录下,而model则是在/usr/lib/lua/luci/model中。而model中有一个特殊的模块叫做CBI,被称为LuCI中最酷的功能,该模块的功能是方便的对一个配置文件进行修改。

三、实例
    1、在/usr/lib/lua/luci/contronller目录下面创建mystar目录并在该目录下面创建mystar.lua脚本文件。

    mystar.lua内容如下:

  1. module("luci.controller.mystar.mystar", package.seeall) --第一个参数对应/usr/lib/lua/luci/controller/mystar/mystar.lua脚本文件
  2.                                                                                                                                     
  3. function index()
  4.         entry({"admin", "network", "mystar_change"}, cbi("mystar-model/mystarmodule"), "Change My Conf", 30).dependent=false 
  5. 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

     2、在/usr/lib/lua/luci/model/cbi/目录下面创建mystar-mode目录并在该目录下面创建mystarmodule.lua脚本文件。

  1. root@OpenWrt:/usr/lib/lua/luci/model/cbi# mkdir mystar-model
  2. root@OpenWrt:/usr/lib/lua/luci/model/cbi# cd mystar-model/
  3. root@OpenWrt:/usr/lib/lua/luci/model/cbi/mystar-model# touch mystarmodule.lua

    mystarmodule.lua内容如下


  1. m = Map("mystar", "mystar conf change interface")   --mystar是对应etc/config目录下面的配置文件名,第二个参数表示子页面标题。
  2. s = m:section(TypedSection, "interface")            --第一个参数固定,第二个参数表示对应于mystar配置文件中interface段
  3. s.addremove = true
  4. s:option(Value, "username", "name:")                --name:是界面提示
  5. key=s:option(Value, "password", "password:")
  6. key.password=true;
  7. return m
    3、在/ect/config目录下面创建mystar配置文件内容如下:

  1. config 'interface' 'mystar'
  2.         option 'username' 'chenfm'
  3.         option 'password' 'admin
   4、测试
    进入openwrt界面,登录后点击network,可以看到我们添加的子页面入口

    

   /etc/config/mystar配置文件修改前
  1. config 'interface' 'mystar'
  2.         option 'username' 'chenfm'
  3.         option 'password' 'admin'
   在上面的界面中输入用户名chenfm 密码123456在点击Save&Apply,回到控制台查看结果如下:
  
  1. config 'interface' 'mystar'
  2.         option 'username' 'chenfm'
  3.         option 'password' '123456
   发现mystar这个配置文件中密码由原来的admin被改成123456



    
 
阅读(6532) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~