3.正则路由
最后更新于:2022-04-02 04:53:18
正则路由
~~~
|-- routers
| `-- router.go
~~~
代码:
~~~
package routers
import (
admin "project/admin/controllers"
"github.com/astaxie/beego"
)
func init() {
// 正则路由
beego.Router("/admin/index", &admin.UserController{}, "*:Index")
beego.Router("/admin/test/?:user_name", &admin.UserController{}, "get:Test")
beego.Router("/admin/test2/:user_name", &admin.UserController{}, "get:Test")
beego.Router("/admin/test3/:user_name:/:userpwd:/:mobile", &admin.UserController{}, "get:Test")
}
~~~
~~~
|-- 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) Test() {
user_name := this.Ctx.Input.Param(":user_name")
values := this.Ctx.Input.Params()
str := fmt.Sprintln(values)
this.Ctx.WriteString("这是正则路由 user/test , values is " + str + " user name is " + user_name)
}
~~~
浏览器访问:
http://127.0.0.1:8080/admin/index
浏览器输出:
这是正则路由 user/index
浏览器访问:
http://127.0.0.1:8080/admin/test/user1
浏览器输出:
这是正则路由 user/test , values is map[:user_name:user1]
user name is user1
浏览器访问:
http://127.0.0.1:8080/admin/test2/user2
浏览器输出:
这是正则路由 user/test , values is map[:user_name:user2]
user name is user2
浏览器访问:
http://127.0.0.1:8080/admin/test3/user3/md5pwd3/15688889999
浏览器输出:
这是正则路由 user/test , values is map[:user_name:user3 :userpwd:md5pwd3 :mobile:15688889999]
user name is user3
';