2.固定路由

最后更新于:2022-04-02 04:53:16

固定路由也就是全匹配的路由 典型的 RESTful 方式 ~~~ |-- routers | `-- router.go ~~~ 代码: ~~~ package routers import ( admin "project/admin/controllers" "github.com/astaxie/beego" ) func init() { // 固定路由也就是全匹配的路由 beego.Router("/admin", &admin.UserController{}, "*:Index") beego.Router("/admin/add/:user_name/:user_pwd:/:mobile", &admin.UserController{}, "get:Insert") beego.Router("/admin/index", &admin.UserController{}, "get:Index") } ~~~ ~~~ |-- admin | |--controllers | `-- user.go ~~~ 代码: ~~~ package admin import ( "fmt" "github.com/astaxie/beego" ) type UserController struct { beego.Controller } func (this *UserController) Index() { this.Ctx.WriteString("这是自动匹配路由 user/index") } func (this *UserController) Insert() { user_name := this.Ctx.Input.Param(":user_name") values := this.Ctx.Input.Params() str := fmt.Sprintln(values) this.Ctx.WriteString("这是自动匹配路由 user/add , values is " + str + " user name is " + user_name) } ~~~ 浏览器访问: http://127.0.0.1:8080/admin 浏览器输出: 这是自动匹配路由 user/index 浏览器访问: http://127.0.0.1:8080/admin/index 浏览器输出: 这是自动匹配路由 user/index 浏览器访问: http://127.0.0.1:8080/admin/add/user1/md5pwd/15688889999 浏览器输出: 这是自动匹配路由 user/add , values is map[:user_name:user1 :user_pwd:md5pwd :mobile:15688889999] user name is user1
';