# tiny **Repository Path**: sixiong/tiny ## Basic Information - **Project Name**: tiny - **Description**: 新手快速入门openresty开发 - **Primary Language**: Lua - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 5 - **Forks**: 1 - **Created**: 2016-07-27 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # tiny openresty blog demo 目的:新手快速入门openresty开发 本demo有借鉴: https://git.oschina.net/love_linger/OmniWeb 可惜本人未能联系上作者; 好了,进入正题 ## 目录 Tiny |++ app 应用目录 | |++ controllers 控制器 | |++ models 模型 | |++ views 视图 | |++ boot 引导执行时初始化, 如验证登陆 | |++ common 公共配置文件 | |-- config.lua 应用配置,默认加载到全局就是_CONF中 | |++ public 入口文件目录 | |----+ css | |----+ js | |----+ upload | |----+ index.lua 单入口 | |++ core 框架核心 | |++ vendor 插件库 | |-- tiny.lua 预加载文件列表及开始执行 | |-- common.lua 内置的常用函数 | |-- controller.lua controller基类 | |-- cookie.lua cookie的实现 | |-- error.lua 默认的错误处理 | |-- model.lua model层的实现 | |-- router.lua 不是路由的路由 | |-- template.lua 导入lua-resty-template库 | ++++++++++ +++++ +++++ +++++ +++++ 控制器方法 +++++ +++++ +++++ +++++ +++++ +++++ +++++ +++++ 1. 创建控制器 Tiny/app/controllers/index.lua 2. 文件内容如下 local index = controller.new(); function index:index() _GET = ngx.req.get_uri_args(); --接收get参数 local page = vendor("page"); --加载分页库 . local m = model.__get("article");--加载模型 . local countNum = tonumber(m:count({})); --总条数 local res ={}; local pageres ={} if countNum > 0 then pageres = page:get(countNum, 15, _GET['page']); --调用分页 res = m:getall({}, pageres['limit']); --得到limit分页值 end self.assign('list', res); --结果传入到view self.assign('page', pageres);--分页传入到view self.assign('title', "首页"); --传入 self.render('zh_cn/index/main.html'); --渲染模板 end return index; ++++++++++ +++++ +++++ +++++ +++++ 模型 +++++ +++++ +++++ +++++ +++++ 1.创建模型 Tiny/app/models/article.lua 2. 文件内容如下 local article = model.__new( _CONF["mysql"] , "mysql"); --mysql 就是mysql 模型 redis 就是redis模型 如果都要用到就再次加载 --加载模型用 local m = model.__get("cache"); function article:getall(where, limit) local str = ""; if where['category_id'] and #(where['category_id']) > 0 then str = str .. ' category_id IN ( '; for k, v in pairs(where['category_id']) do str = str .. v .. ','; end str = string.sub(str, 1, -2); str = str .. ') '; end if string.len(str) >= 3 then str = " WHERE " .. str; end if limit then limit = " ORDER BY id DESC LIMIT " .. limit; end local sql = 'SELECT * FROM ' .. self.table .. str .. limit; -- dump(sql); local res, err, errno, sqlstate = self.mysql(sql); return res; end return article; +++++ +++++ +++++ +++++ +++++ 视图 +++++ +++++ +++++ +++++ +++++ +++++ 1.创建模型 Tiny/app/view/article.lua 2. 文件内容如下