Versions Compared
compared with
Key
- This line was added.
- This line was removed.
- Formatting was changed.
静态分页是指页面的分页参数使用的是路由传参,这种场景下分页对象与Server
的路由定义耦合性比较大。路由定义中需要给定一个page
名称的路由参数,可以使用模糊匹配路由*page
,也可以使用命名匹配路由:page
,也可以使用字段匹配路由{page}
。
示例1,使用模糊匹配路由
Code Block | ||
---|---|---|
| ||
package main import ( "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/ghttp" "github.com/gogf/gf/v2/os/gview" ) func main() { s := g.Server() s.BindHandler("/page/static/*page", func(r *ghttp.Request) { page := r.GetPage(100, 10) buffer, _ := gview.ParseContent(` <html> <head> <style> a,span {padding:8px; font-size:16px;} div{margin:5px 5px 20px 5px} </style> </head> <body> <div>{{.page1}}</div> <div>{{.page2}}</div> <div>{{.page3}}</div> <div>{{.page4}}</div> </body> </html> `, g.Map{ "page1": page.GetContent(1), "page2": page.GetContent(2), "page3": page.GetContent(3), "page4": page.GetContent(4), }) r.Response.Write(buffer) }) s.SetPort(8199) s.Run() } |
...
执行后,我们手动访问 http://127.0.0.1:8199/page/static/6 页面的结果如下:
...
Image Added
示例2,使用字段匹配路由
Code Block | ||
---|---|---|
| ||
package main import ( "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/ghttp" "github.com/gogf/gf/v2/os/gview" ) func main() { s := g.Server() s.BindHandler("/:obj/*action/{page}.html", func(r *ghttp.Request) { page := r.GetPage(100, 10) buffer, _ := gview.ParseContent(` <html> <head> <style> a,span {padding:8px; font-size:16px;} div{margin:5px 5px 20px 5px} </style> </head> <body> <div>{{.page1}}</div> <div>{{.page2}}</div> <div>{{.page3}}</div> <div>{{.page4}}</div> </body> </html> `, g.Map{ "page1": page.GetContent(1), "page2": page.GetContent(2), "page3": page.GetContent(3), "page4": page.GetContent(4), }) r.Response.Write(buffer) }) s.SetPort(8199) s.Run() } |
...
该示例的路由规则更加灵活,其中使用了{page}
字段匹配规则,用于获取当前的分页页码信息。执行后,我们按照路由规则随意访问一个URL如: http://127.0.0.1:8199/order/list/6.html ,结果如下图所示:
...
Image Added
Panel | ||
---|---|---|
| ||
|