我对LUA / LuCI有疑问。我想在功能 createindex 的索引表的末尾添加 mymodule 。
在此代码中:
local controllers = { }
local base = "%s/controller/" % util.libpath()
local _, path
for path in (fs.glob("%s*.lua" % base) or function() end) do
controllers[#controllers+1] = path
end
for path in (fs.glob("%s*/*.lua" % base) or function() end) do
controllers[#controllers+1] = path
end
它会在以下目录lua文件中查找并填充 controller 表,然后在此代码中:
index = {}
for _, path in ipairs(controllers) do
local modname = "luci.controller." .. path:sub(#base+1, #path-4):gsub("/", ".")
local mod = require(modname)
assert(mod ~= true,
"Invalid controller file found\n" ..
"The file '" .. path .. "' contains an invalid module line.\n" ..
"Please verify whether the module name is set to '" .. modname ..
"' - It must correspond to the file path!")
local idx = mod.index
assert(type(idx) == "function",
"Invalid controller file found\n" ..
"The file '" .. path .. "' contains no index() function.\n" ..
"Please make sure that the controller contains a valid " ..
"index function and verify the spelling!")
index[modname] = idx
end
它要求模块顺序正确(如果我在循环中打印 modname ):
luci.controller.firewall
luci.controller.opkg
luci.controller.admin.filebrowser
luci.controller.admin.index
luci.controller.admin.network
luci.controller.admin.status
luci.controller.admin.system
luci.controller.admin.uci
luci.controller.myapp.mymodule
但它使用键字符串(例如“ luci.controller.admin.index”)来设置 index 表。
问题 如果在循环中打印索引表,则结果如下:
1: {"luci.controller.firewall":null}
2: {"luci.controller.firewall":null,"luci.controller.opkg":null}
3: {"luci.controller.firewall":null,"luci.controller.admin.filebrowser":null,"luci.controller.opkg":null}
4: {"luci.controller.firewall":null,"luci.controller.admin.index":null,"luci.controller.admin.filebrowser":null,"luci.controller.opkg":null}
5: {"luci.controller.firewall":null,"luci.controller.admin.index":null,"luci.controller.opkg":null,"luci.controller.admin.filebrowser":null,"luci.controller.admin.network":null}
6: {"luci.controller.firewall":null,"luci.controller.admin.index":null,"luci.controller.opkg":null,"luci.controller.admin.status":null,"luci.controller.admin.filebrowser":null,"luci.controller.admin.network":null}
7: {"luci.controller.firewall":null,"luci.controller.admin.index":null,"luci.controller.opkg":null,"luci.controller.admin.status":null,"luci.controller.admin.system":null,"luci.controller.admin.filebrowser":null,"luci.controller.admin.network":null}
8: {"luci.controller.firewall":null,"luci.controller.admin.index":null,"luci.controller.admin.uci":null,"luci.controller.opkg":null,"luci.controller.admin.status":null,"luci.controller.admin.system":null,"luci.controller.admin.filebrowser":null,"luci.controller.admin.network":null}
9: {"luci.controller.admin.uci":null,"luci.controller.admin.system":null,"luci.controller.myapp.mymodule":null,"luci.controller.firewall":null,"luci.controller.admin.index":null,"luci.controller.admin.network":null,"luci.controller.opkg":null,"luci.controller.admin.filebrowser":null,"luci.controller.admin.status":null}
为什么?为什么这样做呢?
我想在最后一个位置插入 mymodule ,但我不会更改 dispatcher.lua (我希望它保持插入顺序)
** * 更新 * **
我发现,如果我在 mymodule 上更改名称并将其移动到正确的文件夹中(例如luci.controller.admin.test或luci.controller.aa),则我的模块将最后上传
这是一个骗局,但我想知道Lua使用的标准