lapis 路由
2024-09-25 23:18:05
1. 路由以及url 模式
参考如下:
local lapis = require("lapis")
local app = lapis.Application()
app:match("/", function(self) end) // 必须的
app:match("/hello", function(self) end)
app:match("/users/all", function(self) end)
2. 带参数的路由格式
// 包含请求参数的 使用:
app:match("/page/:page", function(self)
print(self.params.page) // 打印输出参数, 比如 /page/dalong 输出 {page:"dalong"}
end)
// 可选路由参数(使用括号)
/projects/:username(/:project)
// * 符号,匹配尽可能多的参数,说白了,就是url 参数中其余的
app:match("/browse/*", function(self)
print(self.params.splat)
end)
app:match("/user/:name/file/*", function(self)
print(self.params.name, self.params.splat)
end)
3. 路由优先级
a. 完全匹配 /hello/world
b. 变量路由 /hello/:variable
c. 统配路由 /hello/*
4. 别名路由(结合self:url_for 类似路由的别名)
// 参考如下:
local lapis = require("lapis")
local app = lapis.Application()
app:match("index", "/", function(self)
return self:url_for("user_profile", { name = "leaf" })
end)
app:match("user_profile", "/user/:name", function(self)
return "Hello " .. self.params.name .. ", go home: " .. self:url_for("index")
end)
最新文章
- JUnit4使用
- php示例代码使用mysql_fetch_assoc函数
- Kruskal算法java版
- 关于UIWebView的总结
- UPC 2224 Boring Counting (离线线段树,统计区间[l,r]之间大小在[A,B]中的数的个数)
- jni编译non-numeric second argument to `wordlist' function错误
- 转:sprintf与snprintf
- 【6】使用nginx
- C++中的引用和移动语义
- JAVA通过I/O流复制文件
- 【原创】大叔经验分享(3)hbase client 如何选择
- redis的 rdb 和 aof 持久化的区别
- Pycharm 中You are using pip version 10.0.1, however version 18.1 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.
- Spring 事务 readOnly 到底是怎么回事?
- <;转>;HTML、CSS、font-family:中文字体的英文名称
- Oracle表字段类型更改的一个经验
- 关于继承中的super()调用父类构造方法
- iOS_20_微博Dock的尾随切换
- 多种移动平均计算总结(MA,EMA,SMA,DMA,TMA,WMA)
- ASP.NET网站发布时的那些坑