自動匹配
用戶首先需要把需要路由的控制器注冊到自動路由中:
beego.AutoRouter(&controllers.MainController{})
那么 beego 就會通過反射獲取該結(jié)構(gòu)體中所有的實現(xiàn)方法,你就可以通過如下的方式訪問到對應(yīng)的方法中:(MainController在路由前面就寫main)
/main/login 調(diào)用 MainController 中的 Login 方法
/main/logout 調(diào)用 MainController 中的 Logout 方法
除了前綴兩個 /:controller/:method 的匹配之外恤批,剩下的 url beego 會幫你自動化解析為參數(shù)独柑,保存在 this.Ctx.Input.Params 當(dāng)中:
請求URL
獲取參數(shù)方法茵乱,當(dāng)通過rest風(fēng)格進行傳遞參數(shù)時桨吊,參數(shù)保存在this.Ctx.Input.Params當(dāng)中,獲取時需通過this.Ctx.Input.Params()[“0”]這種格式才能獲取得到缅茉。
ma.Ctx.WriteString(ma.Ctx.Input.Params()["0"])//2018
我也發(fā)現(xiàn)了
版本1.9.2中并不像官方文檔中說明的那樣可以忽略大小進行方法匹配茵典。
比如,方法為ToPublish,那么在調(diào)用url的過程中傳入toPublish并不能匹配到對應(yīng)的方法宾舅。必須使用topublish才能匹配到Topublish方法。
現(xiàn)在已經(jīng)可以通過自動識別出來下面類似的所有 url彩倚,都會把請求分發(fā)到 controller 的 simple 方法:
/controller/simple
/controller/simple.html
/controller/simple.json
/controller/simple.xml
可以通過 this.Ctx.Input.Param(“:ext”) 獲取后綴名筹我。