Lua 的 MVC 框架:Sailor

jopen 9年前

Sailor 是一个 Lua 语言的 MVC 编程框架。一个快速,功能全面和简单易用的Web开发框架的基础。

目录树信息

  • /docs - this one is supposed to have documentation
  • /src - Lua modules with nice stuff from sailor and other places.
    • /sailor - Sailor modules
    • /sailor/demo-app - default Sailor web app
  • /test - apps for testing and demonstration purposes

支持的环境

Sailor has been tested under Linux, Mac OS X and Windows and is currently compatible with Apache with mod_lua or mod_pLua, Nginx with ngx_lua, or any CGI-enabled web server, like Civetweb or Mongoose, if CGILua is present.

安装

For Linux, see INSTALL_LINUX.md

For Windows, see INSTALL_WIN.md

For Mac, see INSTALL_MAC.md

使用Sailor

默认的Sailor应用程序将具有以下目录树结构:

  • /conf - configuration files, open and edit them.
  • /controllers - controllers you will make!
  • /layouts - default layout files.
  • /models - models you will make!
  • /pub - publicly accessible files (js libraries, for example)
  • /runtime - temporary files generated during runtime.
  • /views - this is where your lua pages in .lp will go

创建页面

Go to /controllers and create your first controller! It should be a lua module. Name it whatever you want, our example is "site.lua". We will serve two pages, one accessible via /?r=site which will run site.index() by default and another one acessible via /?r=site/notindex.

local site = {}  function site.index(page)    local foo = 'Hello world'    local User = sailor.model("user")    local u = User:new()    u.username = "etiene"    u.password = "a_password"    local valid, err = u:validate() -- validate() will check if your attributes follow the rules!    if not valid then      foo = "Boohoo :("    end      -- Warning: this is a tech preview and some methods of model class do not avoid SQL injections yet.    page:render('index',{foo=foo,name=u.username}) -- This will render /views/site/index.lp and pass the variables 'foo' and 'name'  end  function site.notindex(page)    page:write('<b>Hey you!</b>')  end  return site

Go to /views, create a dir named 'site' to match your controller name and create your first page, our example is index.lp

<?=foo?>  <p>    Hi, <?=name?>  </p>


 

项目主页:http://www.open-open.com/lib/view/home/1417313530565