beego路由設(shè)置
beego存在三種方式的路由:固定路由、正則路由、自動(dòng)路由。下面就詳細(xì)說一下如何使用這三種路由。
基礎(chǔ)路由
從 beego 1.2 版本開始支持了基本的 RESTful 函數(shù)式路由,應(yīng)用中的大多數(shù)路由都會(huì)定義在 routers/router.go 文件中。最簡單的 beego 路由由 URI 和閉包函數(shù)組成。
基本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"))
})
可以響應(yīng)任何HTTP請(qǐng)求的路由
beego.Any("/foo",func(ctx *context.Context){
ctx.Output.Body([]byte("bar"))
})
所有的支持的基礎(chǔ)函數(shù):
beego.Get(router, beego.FilterFunc)
beego.Post(router, beego.FilterFunc)
beego.Put(router, beego.FilterFunc)
beego.Patch(router, beego.FilterFunc)
beego.Head(router, beego.FilterFunc)
beego.Options(router, beego.FilterFunc)
beego.Delete(router, beego.FilterFunc)
beego.Any(router, beego.FilterFunc)
固定路由
固定路由也就是全匹配的路由,如下所示:
beego.Router("/", &controllers.MainController{})
beego.Router("/admin", &admin.UserController{})
beego.Router("/admin/index", &admin.ArticleController{})
beego.Router("/admin/addpkg", &admin.AddController{})
如上所示的路由就是我們最常用的路由方式,一個(gè)固定的路由,一個(gè)控制器,然后根據(jù)用戶請(qǐng)求方法不同請(qǐng)求控制器中對(duì)應(yīng)的方法,典型的 RESTful 方式。
RESTful 是一種目前 API 開發(fā)中廣泛采用的形式,beego 默認(rèn)就是支持這樣的請(qǐng)求方法,也就是用戶 Get 請(qǐng)求就執(zhí)行 Get 方法,Post 請(qǐng)求就執(zhí)行 Post 方法。因此默認(rèn)的路由是這樣 RESTful 的請(qǐng)求方式。
正則路由
為了用戶更加方便的路由設(shè)置,beego 參考了 sinatra 的路由實(shí)現(xiàn),支持多種方式的路由:
beego.Router("/api/?:id", &controllers.RController{})
默認(rèn)匹配 //例如對(duì)于URL"/api/123"可以匹配成功,此時(shí)變量":id"值為"123"
beego.Router("/api/:id", &controllers.RController{})
默認(rèn)匹配 //例如對(duì)于URL"/api/123"可以匹配成功,此時(shí)變量":id"值為"123",但URL"/api/"匹配失敗
beego.Router("/api/:id([0-9]+)", &controllers.RController{})
自定義正則匹配 //例如對(duì)于URL"/api/123"可以匹配成功,此時(shí)變量":id"值為"123"
beego.Router("/user/:username([\\w]+)", &controllers.RController{})
正則字符串匹配 //例如對(duì)于URL"/user/astaxie"可以匹配成功,此時(shí)變量":username"值為"astaxie"
beego.Router("/download/*.*", &controllers.RController{})
*匹配方式 //例如對(duì)于URL"/download/file/api.xml"可以匹配成功,此時(shí)變量":path"值為"file/api", ":ext"值為"xml"
beego.Router("/download/ceshi/*", &controllers.RController{})
*全匹配方式 //例如對(duì)于URL"/download/ceshi/file/api.json"可以匹配成功,此時(shí)變量":splat"值為"file/api.json"
beego.Router("/??int", &controllers.RController{})
int 類型設(shè)置方式,匹配 :id為int 類型,框架幫你實(shí)現(xiàn)了正則 ([0-9]+)
beego.Router("/:hi:string", &controllers.RController{})
string 類型設(shè)置方式,匹配 :hi 為 string 類型??蚣軒湍銓?shí)現(xiàn)了正則 ([\w]+)
beego.Router("/cms_:id([0-9]+).html", &controllers.CmsController{})
帶有前綴的自定義正則 //匹配 :id 為正則類型。匹配 cms_123.html 這樣的 url :id = 123
可以在 Controller 中通過如下方式獲取上面的變量:
this.Ctx.Input.Param(":id")
this.Ctx.Input.Param(":username")
this.Ctx.Input.Param(":splat")
this.Ctx.Input.Param(":path")
this.Ctx.Input.Param(":ext")
自定義方法及 RESTful 規(guī)則
上面列舉的是默認(rèn)的請(qǐng)求方法名(請(qǐng)求的 method 和函數(shù)名一致,例如 GET 請(qǐng)求執(zhí)行 Get 函數(shù),POST 請(qǐng)求執(zhí)行 Post 函數(shù)),如果用戶期望自定義函數(shù)名,那么可以使用如下方式:
beego.Router("/",&IndexController{},"*:Index")
使用第三個(gè)參數(shù),第三個(gè)參數(shù)就是用來設(shè)置對(duì)應(yīng) method 到函數(shù)名,定義如下
- *表示任意的 method 都執(zhí)行該函數(shù)
- 使用 httpmethod:funcname 格式來展示
- 多個(gè)不同的格式使用 ; 分割
- 多個(gè) method 對(duì)應(yīng)同一個(gè) funcname,method 之間通過 , 來分割
以下是一個(gè) RESTful 的設(shè)計(jì)示例:
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")
以下是多個(gè) HTTP Method 指向同一個(gè)函數(shù)的示例:
beego.Router("/api",&RestController{},"get,post:ApiFunc")
以下是不同的 method 對(duì)應(yīng)不同的函數(shù),通過 ; 進(jìn)行分割的示例:
beego.Router("/simple",&SimpleController{},"get:GetFunc;post:PostFunc")
可用的 HTTP Method:
*: 包含以下所有的函數(shù)
- get: GET 請(qǐng)求
- post: POST 請(qǐng)求
- put: PUT 請(qǐng)求
- delete: DELETE 請(qǐng)求
- patch: PATCH 請(qǐng)求
- options: OPTIONS 請(qǐng)求
- head: HEAD 請(qǐng)求
如果同時(shí)存在 * 和對(duì)應(yīng)的 HTTP Method,那么優(yōu)先執(zhí)行 HTTP Method 的方法,例如同時(shí)注冊(cè)了如下所示的路由:
beego.Router("/simple",&SimpleController{},"*:AllFunc;post:PostFunc")
那么執(zhí)行 POST 請(qǐng)求的時(shí)候,執(zhí)行 PostFunc 而不執(zhí)行 AllFunc。
自定義函數(shù)的路由默認(rèn)不支持 RESTful 的方法,也就是如果你設(shè)置了 beego.Router("/api",&RestController{},"post:ApiFunc") 這樣的路由,如果請(qǐng)求的方法是 POST,那么不會(huì)默認(rèn)去執(zhí)行 Post 函數(shù)。
自動(dòng)路由
用戶首先需要把需要路由的控制器注冊(cè)到自動(dòng)路由中:
beego.AutoRouter(&controllers.ObjectController{})
那么 beego 就會(huì)通過反射獲取該結(jié)構(gòu)體中所有的實(shí)現(xiàn)方法,你就可以通過如下的方式訪問到對(duì)應(yīng)的方法中:
/object/login 調(diào)用 ObjectController 中的 Login 方法
/object/logout 調(diào)用 ObjectController 中的 Logout 方法
除了前綴兩個(gè) /:controller/:method 的匹配之外,剩下的 url beego 會(huì)幫你自動(dòng)化解析為參數(shù),保存在 this.Ctx.Input.Params 當(dāng)中:
/object/blog/2013/09/12 調(diào)用 ObjectController 中的 Blog 方法,參數(shù)如下:
map[0:2013 1:09 2:12]
方法名在內(nèi)部是保存了用戶設(shè)置的,例如 Login,url 匹配的時(shí)候都會(huì)轉(zhuǎn)化為小寫,所以,/object/LOGIN 這樣的 url 也一樣可以路由到用戶定義的 Login 方法中。
現(xiàn)在已經(jīng)可以通過自動(dòng)識(shí)別出來下面類似的所有 url,都會(huì)把請(qǐng)求分發(fā)到 controller 的 simple 方法:
/controller/simple
/controller/simple.html
/controller/simple.json
/controller/simple.xml
可以通過 this.Ctx.Input.Param(":ext") 獲取后綴名。