回调函数注册

回调函数+执行对象混合注册是推荐的路由注册方式。

这种方式是最简单且最灵活的的路由注册方式,注册的服务可以是一个实例化对象的方法地址,也可以是一个包方法地址。服务需要的数据可以通过模块内部变量形式或者对象内部变量形式进行管理,开发者可根据实际情况进行灵活控制。

我们可以直接通过ghttp.BindHandler方法完成回调函数的注册,在框架的开发手册中很多地方都使用了回调函数注册的方式来做演示,因为这种注册方式比较简单。

使用示例1,与执行对象混合使用

github.com/gogf/gf/blob/master/geg/frame/mvc/controller/demo/product.go

  1. package demo
  2. import (
  3. "github.com/gogf/gf/g"
  4. "github.com/gogf/gf/g/net/ghttp"
  5. )
  6. type Product struct {
  7. total int
  8. }
  9. func init() {
  10. p := &Product{}
  11. g.Server().BindHandler("/product/total", p.Total)
  12. g.Server().BindHandler("/product/list/{page}.html", p.List)
  13. }
  14. func (p *Product) Total(r *ghttp.Request) {
  15. p.total++
  16. r.Response.Write("total: ", p.total)
  17. }
  18. func (p *Product) List(r *ghttp.Request) {
  19. r.Response.Write("page: ", r.Get("page"))
  20. }

在这个示例中,我们使用对象来封装业务逻辑和所需的变量,使用回调函数注册来灵活注册对应的对象方法。

使用示例2,使用包变量管理内部变量

github.com/gogf/gf/blob/master/geg/frame/mvc/controller/stats/stats.go

  1. package stats
  2. import (
  3. "github.com/gogf/gf/g"
  4. "github.com/gogf/gf/g/net/ghttp"
  5. )
  6. var (
  7. total int
  8. )
  9. func init() {
  10. g.Server().BindHandler("/stats/total", showTotal)
  11. }
  12. func showTotal(r *ghttp.Request) {
  13. total++
  14. r.Response.Write("total:", total)
  15. }

使用示例3,请求参数绑定到struct对象

github.com/gogf/gf/blob/master/geg/net/ghttp/server/request/request_struct.go

  1. package main
  2. import (
  3. "github.com/gogf/gf/g"
  4. "github.com/gogf/gf/g/net/ghttp"
  5. )
  6. type User struct {
  7. Uid int `json:"uid"`
  8. Name string `json:"name" params:"username"`
  9. Pass1 string `json:"pass1" params:"password1,userpass1"`
  10. Pass2 string `json:"pass2" params:"password3,userpass2"`
  11. }
  12. func main() {
  13. s := g.Server()
  14. s.BindHandler("/user", func(r *ghttp.Request){
  15. user := new(User)
  16. r.GetToStruct(user)
  17. //r.GetPostToStruct(user)
  18. //r.GetQueryToStruct(user)
  19. r.Response.WriteJson(user)
  20. })
  21. s.SetPort(8199)
  22. s.Run()
  23. }

可以看到,我们可以在定义struct的时候使用params的标签来指定匹配绑定的参数名称,并且支持多个参数名称的绑定,多个参数名称以,号分隔。在使用中我们可以使用GetRequestToStruct/GetPostToStruct/GetQueryToStruct三种方式来获得指定Method提交方式的参数map,此外GetToStructGetRequestToStruct的别名,大多数情况下我们使用该方式获取参数即可。

如果是其他方式提交参数,如果Json/Xml等等,由于设计到自定义参数格式的解析再绑定,请参考gconv模块map转换struct的标签名称gconv用法示例

以上示例执行后,我们手动访问地址http://127.0.0.1:8199/user?uid=1&name=john&password1=123&userpass2=123,输出结果为:

  1. {"name":"john","pass1":"123","pass2":"123","uid":1}

使用示例4,请求参数绑定+数据校验示例

github.com/gogf/gf/blob/master/geg/net/ghttp/server/request/request_validation.go

  1. package main
  2. import (
  3. "github.com/gogf/gf/g"
  4. "github.com/gogf/gf/g/net/ghttp"
  5. "github.com/gogf/gf/g/util/gvalid"
  6. )
  7. func main() {
  8. type User struct {
  9. Uid int `gvalid:"uid@min:1"`
  10. Name string `params:"username" gvalid:"username @required|length:6,30"`
  11. Pass1 string `params:"password1" gvalid:"password1@required|password3"`
  12. Pass2 string `params:"password2" gvalid:"password2@required|password3|same:password1#||两次密码不一致,请重新输入"`
  13. }
  14. s := g.Server()
  15. s.BindHandler("/user", func(r *ghttp.Request){
  16. user := new(User)
  17. r.GetToStruct(user)
  18. if err := gvalid.CheckStruct(user, nil); err != nil {
  19. r.Response.WriteJson(err.Maps())
  20. } else {
  21. r.Response.Write("ok")
  22. }
  23. })
  24. s.SetPort(8199)
  25. s.Run()
  26. }

其中,

  1. 这里使用了r.GetToStruct(user)方法将请求参数绑定到指定的user对象上,注意这里的userUser的结构体实例化指针;在struct tag中,使用params标签来指定参数名称与结构体属性名称对应关系;
  2. 通过gvalid标签为gavlid数据校验包特定的校验规则标签,具体请参考【数据校验】章节;其中,密码字段的校验规则为password3,表示: 密码格式为任意6-18位的可见字符,必须包含大小写字母、数字和特殊字符
  3. 当校验染回结果非nil时,表示校验不通过,这里使用r.Response.WriteJson方法返回json结果;

执行后,试着访问URL: http://127.0.0.1:8199/user?uid=1&name=john&password1=123&password2=456,输出结果为:

  1. {"password1":{"password3":"密码格式不合法,密码格式为任意6-18位的可见字符,必须包含大小写字母、数字和特殊字符"},"password2":{"password3":"密码格式不合法,密码格式为任意6-18位的可见字符,必须包含大小写字母、数字和特殊字符","same":"两次密码不一致,请重新输入"},"username":{"length":"字段长度为6到30个字符"}}

假如我们访问访问URL: http://127.0.0.1:8199/user?uid=1&name=johng-cn&password1=John$123&password2=John$123,输出结果为:

  1. ok