beego的路由机制

基本 GET 路由

beego.Get("/",func(ctx *context.Context){
     ctx.Output.Body([]byte("hello world"))
})

基本 POST 路由

beego.Post("/alice",func(ctx *context.Context){
     ctx.Output.Body([]byte("bob"))
})

注册一个可以响应任何 HTTP 的路由

beego.Any("/foo",func(ctx *context.Context){
     ctx.Output.Body([]byte("bar"))
})

自定义方法及 RESTful 规则

beego.Router("/api/list",&RestController{},"*:ListFood")
beego.Router("/api/create",&RestController{},"post:CreateFood")
beego.Router("/api/update",&RestController{},"put:UpdateFood")
beego.Router("/api/delete",&RestController{},"delete:DeleteFood")

以下是多个 HTTP Method 指向同一个函数的示例:

beego.Router("/api",&RestController{},"get,post:ApiFunc")

以下是不同的 method 对应不同的函数,通过 ; 进行分割的示例:

beego.Router("/simple",&SimpleController{},"get:GetFunc;post:PostFunc")

自动匹配

    beego.InsertFilter("/back/achievement/*", beego.BeforeRouter, func(ctx *context.Context) { //路由过滤
        _, ok := ctx.Input.Session("type").(string)
        if !ok {
            ctx.Redirect(302, "/back/backlogin")
        }
    })
    back := beego.NewNamespace("back",
        beego.NSInclude(
            &backstage.LoginController{},
            &backstage.IndexController{},
            &backstage.ScroeController{},
            &backstage.IdiomController{},
            &backstage.ItemController{},
            &backstage.AdminController{},
            &backstage.TipsController{},
        ),
    )
    beego.AddNamespace(back)
//后台登陆
//@router /backlogin [get]
func (this *LoginController)Login()  {
    this.TplName = "backstage/login.html"
}
//@router /leavelogin [get]
func (this *LoginController)LeaveLogin()  {
    this.DestroySession()
    this.TplName = "backstage/login.html"
}

访问方式 http://localhost:9999/back/leavelogin

你可能感兴趣的:(beego的路由机制)