本文说一下在设置路由时可以使用的笔记
1. 路由器的设置
代码如下:
webns := beego.NewNamespace("/web", beego.NSRouter("/login", &logincontrollers.LoginWebController{}, "get:Login"), ) beego.AddNamespace(webns) //beego.Router("/web/login", &logincontrollers.LoginWebController{}, "get:Login")
大家可以看到,两个路由器设置,功能是一样的,但是上面的那个多的是namespace(这个应该都知道!),并且里面的函数也不一样。使用http://127.0.0.1:8080/web/login 就能打开此链接
2. 正则路由
这个我也是不懂,就举两个简单的,可行的例子,其它的请上beego查看
beego.Router("/web/login/:id", &logincontrollers.LoginWebController{}, "get:Login") beego.Router("/web/login/?:id", &logincontrollers.LoginWebController{}, "get:Login")
路由1:
http://127.0.0.1:8080/web/login/123 可以访问
http://127.0.0.1:8080/web/login 不能访问
路由2:
http://127.0.0.1:8080/web/login/123 可以访问
http://127.0.0.1:8080/web/login 可以访问
像我一样设置这两个路由,路由1是白设置的,因为路由2是兼容路由1的。
我们可以beego.Controller的Ctx.Input.Param(“:id”) 获取值
3. url 的其它值获取
这里指的是如:
http://127.0.0.1:8080/web/login?abc=10abca&age=2000
怎么读取“?”后的数据呢?
var abc string if err := l.Ctx.Input.Bind(&abc, "abc"); err != nil { log.Println("get abc error:", err) } else { log.Println("get [abc] = ", abc) } var age string if err := l.Ctx.Input.Bind(&age, "age"); err != nil { log.Println("get age error:", err) } else { log.Println("get [age] = ", age) }
这样就可以获取到了