一、Controller 控制器
Controller等同於Django里的view,處理邏輯都是在Controller里面完成的,下面就寫一個最簡單的Controller。
寫controller的時候,一定要繼承beego.Controller,也一定要記得導入 github.com/astaxie/beego.
1、 基於 beego 的 Controller 設計,只需要匿名組合 beego.Controller。
示例1: 語法
type xxxController struct { beego.Controller }
示例2: 使用方法
package controllers import ( "github.com/astaxie/beego" ) type MainController struct { beego.Controller } func (this *MainController) Get() { this.Data["Website"] = "beego.me" this.Data["Email"] = "astaxie@gmail.com" this.TplName = "index.tpl" }
3、Controller用法介紹
beego.Controller 擁有很多方法,其中包括 Init、Prepare、Post、Get、Delete、Head等 方法。我們可以通過重寫的方式來實現這些方法,而我們上面的代碼就是重寫了 Get 方法。
我們可以通過各種方式獲取數據,然后賦值到 this.Data 中,這是一個用來存儲輸出數據的 map,可以賦值任意類型的值,這里我們只是簡單舉例輸出兩個字符串。
最后一個就是需要去渲染的模板,this.TplName 就是需要渲染的模板,這里指定了 index.tpl,如果用戶不設置該參數,那么默認會去到模板目錄的 Controller/<方法名>.tpl 查找,例如上面的方法會去 maincontroller/get.tpl(文件、文件夾必須小寫)。
用戶設置了模板之后系統會自動的調用 Render 函數(這個函數是在 beego.Controller 中實現的),所以無需用戶自己來調用渲染。
示例: 當然也可以不使用模版,直接用 this.Ctx.WriteString
輸出字符串
func (this *MainController) Get() { this.Ctx.WriteString("hello") }
4、beego.Controller源碼解讀
示例:
type Controller struct { // context data Ctx *context.Context Data map[interface{}]interface{} // route controller info controllerName string actionName string methodMapping map[string]func() //method:routertree gotofunc string AppController interface{} // template data TplName string ViewPath string Layout string LayoutSections map[string]string // the key is the section name and the value is the template name TplPrefix string TplExt string EnableRender bool // xsrf data _xsrfToken string XSRFExpire int EnableXSRF bool // session CruSession session.Store } // ControllerInterface is an interface to uniform all controller handler. type ControllerInterface interface { Init(ct *context.Context, controllerName, actionName string, app interface{}) Prepare() Get() Post() Delete() Put() Head() Patch() Options() Finish() Render() error XSRFToken() string CheckXSRFCookie() bool HandlerFunc(fn string) bool URLMapping() }
beego.Controller 實現了接口beego.ControllerInterface,beego.ControllerInterface 定義了如下函數:
Init(ct *context.Context, childName string, app interface{})
這個函數主要初始化了 Context、相應的 Controller 名稱,模板名,初始化模板參數的容器 Data,app 即為當前執行的 Controller 的 reflecttype,這個 app 可以用來執行子類的方法。
Prepare()
這個函數主要是為了用戶擴展用的,這個函數會在下面定義的這些 Method 方法之前執行,用戶可以重寫這個函數實現類似用戶驗證之類。
Get()
如果用戶請求的 HTTP Method 是 GET,那么就執行該函數,默認是 405,用戶繼承的子 struct 中可以實現了該方法以處理 Get 請求。
Post()
如果用戶請求的 HTTP Method 是 POST,那么就執行該函數,默認是 405,用戶繼承的子 struct 中可以實現了該方法以處理 Post 請求。
Delete()
如果用戶請求的 HTTP Method 是 DELETE,那么就執行該函數,默認是 405,用戶繼承的子 struct 中可以實現了該方法以處理 Delete 請求。
Put()
如果用戶請求的 HTTP Method 是 PUT,那么就執行該函數,默認是 405,用戶繼承的子 struct 中可以實現了該方法以處理 Put 請求.
Head()
如果用戶請求的 HTTP Method 是 HEAD,那么就執行該函數,默認是 405,用戶繼承的子 struct 中可以實現了該方法以處理 Head 請求。
Patch()
如果用戶請求的 HTTP Method 是 PATCH,那么就執行該函數,默認是 405,用戶繼承的子 struct 中可以實現了該方法以處理 Patch 請求.
Options()
如果用戶請求的HTTP Method是OPTIONS,那么就執行該函數,默認是 405,用戶繼承的子 struct 中可以實現了該方法以處理 Options 請求。
Finish()
這個函數是在執行完相應的 HTTP Method 方法之后執行的,默認是空,用戶可以在子 struct 中重寫這個函數,執行例如數據庫關閉,清理數據之類的工作。
Render() error
這個函數主要用來實現渲染模板,如果 beego.AutoRender 為 true 的情況下才會執行。