local I18nUtil = {}
function I18nUtil:_(key, params)
return key
end
return I18nUtil
local i18nUtil = require "util.i18n"
i18nUtil:_("hello word")
在 13-7-26,范洪毅<foyo...@gmail.com> 写道:
> 我在使用module 开发的时候 遇到需要向 require文件传递参数(变量),在模块中如何接受参数 或者返回参数,能否给一个demo?
>
> 53 --ngx.exec("/"..postdata.class, {uid = uid, gameid = gameid, platform =
> platform, exp = exp, protocol = protocol, promoterid = promoterid, data =
> data})
> 54 local filename = postdata.class --引入脚本名
> 55 local fuc = postdata.method --执行脚本(模块的方法)
> 56
> 57
> 59 local opt = require (filename)
> 60
> 61 ngx.say(filename,"=>" ,fuc,"=>",data )
> 62 --ngx.exit(200)
> 63 --_G[fuc](uid, gameid, exp, promoterid, platform, protocol, postdata)
> 64
> 65 local r = opt.new()
> 66 local back = r:route("postparams") --调取引入文件的route函数,传递参数
> 67 ngx.say(cjson.encode(back)) --获取返回值
>
> 返回这样{"sock":{}}
>
>
>
> --require 脚本代码
>
>
>
> local mt = { __index = get_ware_dict }
> 183
> 184 function test()
> 185 return "fucek"
> 186 end
> 187
> 188 function new(self)
> 189 return setmetatable({ sock = tcp() }, mt)
> 190 end
> 191
> 192
> 193 function route(uid)
> 194 local u = uid
> 195 return u
> 196 end
> 197
> 198 local class_mt = {
> 199 -- to prevent use of casual module global variables
> 200 __newindex = function (table, key, val)
> 201 error('attempt to write to undeclared variable "' .. key ..
> '"')
> 202 end
> 203 }
> 204 setmetatable(_M, class_mt)
>
> 在 2013年7月26日星期五UTC+8下午4时34分33秒,Xiaohui Sun写道:
>>
>> 你这个貌似是我最早使用openresty写的一个demo
>> 当时用了云风的Class遇到的问题是openresty会在每次请求的时候清空_G
>> 基本上就杜绝了全局变量使用的可能性
>> 后来我进行了重构,全使用module的方式,但未更新github
>> 推荐你使用module开发
>> 变量使用用local
>>
>> 在 13-7-26,范洪毅<foyo...@gmail.com <javascript:>> 写道:
>> > 当lua_code_cache off时,路由没有问题,
>> > 当lua_code_cache on 时,
>> > 也设置了local了
>> >
>> > 2013/07/26 12:20:25 [error] 1380#0: *7 lua entry thread aborted: runtime
>> >
>> > error: /usr/local/openresty/lualib/lib/core/Test.lua:1: attempt to call
>> >
>> > global 'Class' (a nil value)
>> > 哪位在写路由 的时候,遇到过这样的问题,帮忙调试下?
>> > 非常感谢
>> >
>> > --
>> > --
>