Lua 的 MVC 框架:Sailor

jopen 10年前發布 | 38K 次閱讀 Sailor Web框架

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.baiduhome.net/lib/view/home/1417313530565

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!