<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## 路由設置 ### ## 正則路由 1、正則路由 為了用戶更加方便的路由設置,beego 參考了 sinatra 的路由實現,支持多種方式的路由: ~~~ beego.Router(“/api/?:id”, &controllers.RController{}) ~~~ 默認匹配 //匹配 /api/123 :id = 123 可以匹配 /api/ 這個URL ~~~ beego.Router(“/api/:id”, &controllers.RController{}) ~~~ 默認匹配 //匹配 /api/123 :id = 123 不可以匹配 /api/ 這個URL ~~~ beego.Router(“/api/:id([0-9]+)“, &controllers.RController{}) ~~~ 自定義正則匹配 //匹配 /api/123 :id = 123 ~~~ beego.Router(“/user/:username([\w]+)“, &controllers.RController{}) ~~~ 正則字符串匹配 //匹配 /user/astaxie :username = astaxie ~~~ beego.Router(“/download/.”, &controllers.RController{}) ~~~ \*匹配方式 //匹配 /download/file/api.xml :path= file/api :ext=xml ~~~ beego.Router(“/download/ceshi/*“, &controllers.RController{}) ~~~ \*全匹配方式 //匹配 /download/ceshi/file/api.json :splat=file/api.json ~~~ beego.Router(“/:id:int”, &controllers.RController{}) ~~~ int 類型設置方式,匹配 :id為int 類型,框架幫你實現了正則 (\[0-9\]+) ~~~ beego.Router(“/:hi:string”, &controllers.RController{}) ~~~ string 類型設置方式,匹配 :hi 為 string 類型。框架幫你實現了正則 (\[\\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 規則 上面列舉的是默認的請求方法名(請求的 method 和函數名一致,例如 `GET` 請求執行 `Get` 函數,`POST` 請求執行 `Post` 函數),如果用戶期望自定義函數名,那么可以使用如下方式: ~~~ beego.Router("/",&IndexController{},"*:Index") ~~~ 使用第三個參數,第三個參數就是用來設置對應 method 到函數名,定義如下 * `*`表示任意的 method 都執行該函數 * 使用 httpmethod:funcname 格式來展示 * 多個不同的格式使用 `;` 分割 * 多個 method 對應同一個 funcname,method 之間通過 `,` 來分割 以下是一個 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") ~~~ 可用的 HTTP Method: * \*: 包含以下所有的函數 * get: GET 請求 * post: POST 請求 * put: PUT 請求 * delete: DELETE 請求 * patch: PATCH 請求 * options: OPTIONS 請求 * head: HEAD 請求 如果同時存在 \* 和對應的 HTTP Method,那么優先執行 HTTP Method 的方法,例如同時注冊了如下所示的路由: ~~~ beego.Router("/simple",&SimpleController{},"*:AllFunc;post:PostFunc") ~~~ 那么執行 `POST` 請求的時候,執行 `PostFunc` 而不執行 `AllFunc`。 > > > 自定義函數的路由默認不支持 RESTful 的方法,也就是如果你設置了 `beego.Router("/api",&RestController{},"post:ApiFunc")` 這樣的路由,如果請求的方法是 `POST`,那么不會默認去執行 `Post` 函數。 ## ## 自動匹配 用戶首先需要把需要路由的控制器注冊到自動路由中: ~~~ beego.AutoRouter(&controllers.ObjectController{}) ~~~ 那么 beego 就會通過反射獲取該結構體中所有的實現方法,你就可以通過如下的方式訪問到對應的方法中: ~~~ /object/login ? 調用 ObjectController 中的 Login 方法 /object/logout ?調用 ObjectController 中的 Logout 方法 ~~~ 除了前綴兩個 `/:controller/:method` 的匹配之外,剩下的 url beego 會幫你自動化解析為參數,保存在 `this.Ctx.Input.Params` 當中: ~~~ /object/blog/2013/09/12 ?調用 ObjectController 中的 Blog 方法,參數如下:map[0:2013 1:09 2:12] ~~~ 方法名在內部是保存了用戶設置的,例如 Login,url 匹配的時候都會轉化為小寫,所以,`/object/LOGIN` 這樣的 `url` 也一樣可以路由到用戶定義的 `Login` 方法中。 現在已經可以通過自動識別出來下面類似的所有 url,都會把請求分發到 `controller` 的 `simple` 方法: ~~~ /controller/simple /controller/simple.html /controller/simple.json /controller/simple.xml ~~~ 可以通過 `this.Ctx.Input.Param(":ext")` 獲取后綴名。 ## 注解路由 從 beego 1.3 版本開始支持了注解路由,用戶無需在 router 中注冊路由,只需要 Include 相應地 controller,然后在 controller 的 method 方法上面寫上 router 注釋(// @router)就可以了,詳細的使用請看下面的例子: ~~~ // CMS API type CMSController struct { ? ?beego.Controller } ? func (c *CMSController) URLMapping() { ? ?c.Mapping("StaticBlock", c.StaticBlock) ? ?c.Mapping("AllBlock", c.AllBlock) } ? ? // @router /staticblock/:key [get] func (this *CMSController) StaticBlock() { ? } ? // @router /all/:key [get] func (this *CMSController) AllBlock() { ? } ~~~ 可以在 `router.go` 中通過如下方式注冊路由: ~~~ beego.Include(&CMSController{}) ~~~ beego 自動會進行源碼分析,注意只會在 dev 模式下進行生成,生成的路由放在 “/routers/commentsRouter.go” 文件中。 這樣上面的路由就支持了如下的路由: * GET /staticblock/:key * GET /all/:key 其實效果和自己通過 Router 函數注冊是一樣的: ~~~ beego.Router("/staticblock/:key", &CMSController{}, "get:StaticBlock") beego.Router("/all/:key", &CMSController{}, "get:AllBlock") ~~~ 同時大家注意到新版本里面增加了 URLMapping 這個函數,這是新增加的函數,用戶如果沒有進行注冊,那么就會通過反射來執行對應的函數,如果注冊了就會通過 interface 來進行執行函數,性能上面會提升很多。 ## namespace ~~~ //初始化 namespace ns := beego.NewNamespace("/v1", ? ?beego.NSCond(func(ctx *context.Context) bool { ? ? ? ?if ctx.Input.Domain() == "api.beego.me" { ? ? ? ? ? ?return true ? ? ? } ? ? ? ?return false ? }), ? ?beego.NSBefore(auth), ? ?beego.NSGet("/notallowed", func(ctx *context.Context) { ? ? ? ?ctx.Output.Body([]byte("notAllowed")) ? }), ? ?beego.NSRouter("/version", &AdminController{}, "get:ShowAPIVersion"), ? ?beego.NSRouter("/changepassword", &UserController{}), ? ?beego.NSNamespace("/shop", ? ? ? ?beego.NSBefore(sentry), ? ? ? ?beego.NSGet("/:id", func(ctx *context.Context) { ? ? ? ? ? ?ctx.Output.Body([]byte("notAllowed")) ? ? ? }), ? ), ? ?beego.NSNamespace("/cms", ? ? ? ?beego.NSInclude( ? ? ? ? ? ?&controllers.MainController{}, ? ? ? ? ? ?&controllers.CMSController{}, ? ? ? ? ? ?&controllers.BlockController{}, ? ? ? ), ? ), ) //注冊 namespace beego.AddNamespace(ns) ~~~ 上面這個代碼支持了如下這樣的請求 URL
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看