add indent
Showing
1 changed file
with
93 additions
and
93 deletions
| ... | @@ -17,25 +17,25 @@ To install: | ... | @@ -17,25 +17,25 @@ To install: |
| 17 | ============ | 17 | ============ |
| 18 | Here is the canonical "Hello, world" example app for beego: | 18 | Here is the canonical "Hello, world" example app for beego: |
| 19 | ```go | 19 | ```go |
| 20 | package main | 20 | package main |
| 21 | 21 | ||
| 22 | import ( | 22 | import ( |
| 23 | "github.com/astaxie/beego" | 23 | "github.com/astaxie/beego" |
| 24 | ) | 24 | ) |
| 25 | 25 | ||
| 26 | type MainController struct { | 26 | type MainController struct { |
| 27 | beego.Controller | 27 | beego.Controller |
| 28 | } | 28 | } |
| 29 | 29 | ||
| 30 | func (this *MainController) Get() { | 30 | func (this *MainController) Get() { |
| 31 | this.Ctx.WriteString("hello world") | 31 | this.Ctx.WriteString("hello world") |
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | func main() { | 34 | func main() { |
| 35 | beego.RegisterController("/", &MainController{}) | 35 | beego.RegisterController("/", &MainController{}) |
| 36 | //beego.HttpPort = 8080 // default | 36 | //beego.HttpPort = 8080 // default |
| 37 | beego.Run() | 37 | beego.Run() |
| 38 | } | 38 | } |
| 39 | ``` | 39 | ``` |
| 40 | 40 | ||
| 41 | http get http://localhost:8080/ | 41 | http get http://localhost:8080/ |
| ... | @@ -54,16 +54,16 @@ Some associated tools for beego reside in:[bee](https://github.com/astaxie/bee) | ... | @@ -54,16 +54,16 @@ Some associated tools for beego reside in:[bee](https://github.com/astaxie/bee) |
| 54 | ============ | 54 | ============ |
| 55 | In beego, a route is a struct paired with a URL-matching pattern. The struct has many method with the same name of http method to serve the http response. Each route is associated with a block. | 55 | In beego, a route is a struct paired with a URL-matching pattern. The struct has many method with the same name of http method to serve the http response. Each route is associated with a block. |
| 56 | ```go | 56 | ```go |
| 57 | beego.RegisterController("/", &controllers.MainController{}) | 57 | beego.RegisterController("/", &controllers.MainController{}) |
| 58 | beego.RegisterController("/admin", &admin.UserController{}) | 58 | beego.RegisterController("/admin", &admin.UserController{}) |
| 59 | beego.RegisterController("/admin/index", &admin.ArticleController{}) | 59 | beego.RegisterController("/admin/index", &admin.ArticleController{}) |
| 60 | beego.RegisterController("/admin/addpkg", &admin.AddController{}) | 60 | beego.RegisterController("/admin/addpkg", &admin.AddController{}) |
| 61 | ``` | 61 | ``` |
| 62 | You can specify custom regular expressions for routes: | 62 | You can specify custom regular expressions for routes: |
| 63 | ```go | 63 | ```go |
| 64 | beego.RegisterController("/admin/editpkg/:id([0-9]+)", &admin.EditController{}) | 64 | beego.RegisterController("/admin/editpkg/:id([0-9]+)", &admin.EditController{}) |
| 65 | beego.RegisterController("/admin/delpkg/:id([0-9]+)", &admin.DelController{}) | 65 | beego.RegisterController("/admin/delpkg/:id([0-9]+)", &admin.DelController{}) |
| 66 | beego.RegisterController("/:pkg(.*)", &controllers.MainController{}) | 66 | beego.RegisterController("/:pkg(.*)", &controllers.MainController{}) |
| 67 | ``` | 67 | ``` |
| 68 | You can also create routes for static files: | 68 | You can also create routes for static files: |
| 69 | 69 | ||
| ... | @@ -77,35 +77,35 @@ You can apply filters to routes, which is useful for enforcing security, redirec | ... | @@ -77,35 +77,35 @@ You can apply filters to routes, which is useful for enforcing security, redirec |
| 77 | 77 | ||
| 78 | You can, for example, filter all request to enforce some type of security: | 78 | You can, for example, filter all request to enforce some type of security: |
| 79 | ```go | 79 | ```go |
| 80 | var FilterUser = func(w http.ResponseWriter, r *http.Request) { | 80 | var FilterUser = func(w http.ResponseWriter, r *http.Request) { |
| 81 | if r.URL.User == nil || r.URL.User.Username() != "admin" { | 81 | if r.URL.User == nil || r.URL.User.Username() != "admin" { |
| 82 | http.Error(w, "", http.StatusUnauthorized) | 82 | http.Error(w, "", http.StatusUnauthorized) |
| 83 | } | 83 | } |
| 84 | } | 84 | } |
| 85 | 85 | ||
| 86 | beego.Filter(FilterUser) | 86 | beego.Filter(FilterUser) |
| 87 | ``` | 87 | ``` |
| 88 | You can also apply filters only when certain REST URL Parameters exist: | 88 | You can also apply filters only when certain REST URL Parameters exist: |
| 89 | ```go | 89 | ```go |
| 90 | beego.RegisterController("/:id([0-9]+)", &admin.EditController{}) | 90 | beego.RegisterController("/:id([0-9]+)", &admin.EditController{}) |
| 91 | beego.FilterParam("id", func(rw http.ResponseWriter, r *http.Request) { | 91 | beego.FilterParam("id", func(rw http.ResponseWriter, r *http.Request) { |
| 92 | ... | 92 | ... |
| 93 | }) | 93 | }) |
| 94 | ``` | 94 | ``` |
| 95 | Additionally, You can apply filters only when certain prefix URL path exist: | 95 | Additionally, You can apply filters only when certain prefix URL path exist: |
| 96 | ```go | 96 | ```go |
| 97 | beego.FilterPrefixPath("/admin", func(rw http.ResponseWriter, r *http.Request) { | 97 | beego.FilterPrefixPath("/admin", func(rw http.ResponseWriter, r *http.Request) { |
| 98 | … auth | 98 | … auth |
| 99 | }) | 99 | }) |
| 100 | ``` | 100 | ``` |
| 101 | 101 | ||
| 102 | ## Controller / Struct | 102 | ## Controller / Struct |
| 103 | ============ | 103 | ============ |
| 104 | To implement a beego Controller, embed the `beego.Controller` struct: | 104 | To implement a beego Controller, embed the `beego.Controller` struct: |
| 105 | ```go | 105 | ```go |
| 106 | type xxxController struct { | 106 | type xxxController struct { |
| 107 | beego.Controller | 107 | beego.Controller |
| 108 | } | 108 | } |
| 109 | ``` | 109 | ``` |
| 110 | `beego.Controller` satisfieds the `beego.ControllerInterface` interface, which defines the following methods: | 110 | `beego.Controller` satisfieds the `beego.ControllerInterface` interface, which defines the following methods: |
| 111 | 111 | ||
| ... | @@ -156,40 +156,40 @@ To implement a beego Controller, embed the `beego.Controller` struct: | ... | @@ -156,40 +156,40 @@ To implement a beego Controller, embed the `beego.Controller` struct: |
| 156 | 156 | ||
| 157 | So you can define ChildStruct method to accomplish the interface's method, now let us see an example: | 157 | So you can define ChildStruct method to accomplish the interface's method, now let us see an example: |
| 158 | ```go | 158 | ```go |
| 159 | type AddController struct { | 159 | type AddController struct { |
| 160 | beego.Controller | 160 | beego.Controller |
| 161 | } | 161 | } |
| 162 | 162 | ||
| 163 | func (this *AddController) Prepare() { | 163 | func (this *AddController) Prepare() { |
| 164 | 164 | ||
| 165 | } | 165 | } |
| 166 | 166 | ||
| 167 | func (this *AddController) Get() { | 167 | func (this *AddController) Get() { |
| 168 | this.Layout = "admin/layout.html" | 168 | this.Layout = "admin/layout.html" |
| 169 | this.TplNames = "admin/add.tpl" | 169 | this.TplNames = "admin/add.tpl" |
| 170 | } | 170 | } |
| 171 | 171 | ||
| 172 | func (this *AddController) Post() { | 172 | func (this *AddController) Post() { |
| 173 | //data deal with | 173 | //data deal with |
| 174 | this.Ctx.Request.ParseForm() | 174 | this.Ctx.Request.ParseForm() |
| 175 | pkgname := this.Ctx.Request.Form.Get("pkgname") | 175 | pkgname := this.Ctx.Request.Form.Get("pkgname") |
| 176 | content := this.Ctx.Request.Form.Get("content") | 176 | content := this.Ctx.Request.Form.Get("content") |
| 177 | beego.Info(this.Ctx.Request.Form) | 177 | beego.Info(this.Ctx.Request.Form) |
| 178 | pk := models.GetCruPkg(pkgname) | 178 | pk := models.GetCruPkg(pkgname) |
| 179 | if pk.Id == 0 { | 179 | if pk.Id == 0 { |
| 180 | var pp models.PkgEntity | 180 | var pp models.PkgEntity |
| 181 | pp.Pid = 0 | 181 | pp.Pid = 0 |
| 182 | pp.Pathname = pkgname | 182 | pp.Pathname = pkgname |
| 183 | pp.Intro = pkgname | 183 | pp.Intro = pkgname |
| 184 | models.InsertPkg(pp) | 184 | models.InsertPkg(pp) |
| 185 | pk = models.GetCruPkg(pkgname) | 185 | pk = models.GetCruPkg(pkgname) |
| 186 | } | ||
| 187 | var at models.Article | ||
| 188 | at.Pkgid = pk.Id | ||
| 189 | at.Content = content | ||
| 190 | models.InsertArticle(at) | ||
| 191 | this.Ctx.Redirect(302, "/admin/index") | ||
| 192 | } | 186 | } |
| 187 | var at models.Article | ||
| 188 | at.Pkgid = pk.Id | ||
| 189 | at.Content = content | ||
| 190 | models.InsertArticle(at) | ||
| 191 | this.Ctx.Redirect(302, "/admin/index") | ||
| 192 | } | ||
| 193 | ``` | 193 | ``` |
| 194 | ## View / Template | 194 | ## View / Template |
| 195 | ============ | 195 | ============ |
| ... | @@ -220,14 +220,14 @@ In the controller you needn't to call render function. beego will auto call this | ... | @@ -220,14 +220,14 @@ In the controller you needn't to call render function. beego will auto call this |
| 220 | 220 | ||
| 221 | You can disable automatic invokation of autorender via the AutoRender Flag: | 221 | You can disable automatic invokation of autorender via the AutoRender Flag: |
| 222 | ```go | 222 | ```go |
| 223 | beego.AutoRender = false | 223 | beego.AutoRender = false |
| 224 | ``` | 224 | ``` |
| 225 | 225 | ||
| 226 | ### layout | 226 | ### layout |
| 227 | beego supports layouts for views. For example: | 227 | beego supports layouts for views. For example: |
| 228 | ```go | 228 | ```go |
| 229 | this.Layout = "admin/layout.html" | 229 | this.Layout = "admin/layout.html" |
| 230 | this.TplNames = "admin/add.tpl" | 230 | this.TplNames = "admin/add.tpl" |
| 231 | ``` | 231 | ``` |
| 232 | 232 | ||
| 233 | In layout.html you must define the variable like this to show sub template's content: | 233 | In layout.html you must define the variable like this to show sub template's content: |
| ... | @@ -239,12 +239,12 @@ beego first parses the TplNames files, renders their content, and appends it to | ... | @@ -239,12 +239,12 @@ beego first parses the TplNames files, renders their content, and appends it to |
| 239 | ### template function | 239 | ### template function |
| 240 | beego support users to define template function like this: | 240 | beego support users to define template function like this: |
| 241 | ```go | 241 | ```go |
| 242 | func hello(in string)(out string){ | 242 | func hello(in string)(out string){ |
| 243 | out = in + "world" | 243 | out = in + "world" |
| 244 | return | 244 | return |
| 245 | } | 245 | } |
| 246 | 246 | ||
| 247 | beego.AddFuncMap("hi",hello) | 247 | beego.AddFuncMap("hi",hello) |
| 248 | ``` | 248 | ``` |
| 249 | 249 | ||
| 250 | then in you template you can use it like this: | 250 | then in you template you can use it like this: |
| ... | @@ -270,17 +270,17 @@ You can use `beego.Controller.ServeJson` or `beego.Controller.ServeXml` for seri | ... | @@ -270,17 +270,17 @@ You can use `beego.Controller.ServeJson` or `beego.Controller.ServeXml` for seri |
| 270 | 270 | ||
| 271 | Helper function for serving Json, sets content type to application/json: | 271 | Helper function for serving Json, sets content type to application/json: |
| 272 | ```go | 272 | ```go |
| 273 | func (this *AddController) Get() { | 273 | func (this *AddController) Get() { |
| 274 | mystruct := { ... } | 274 | mystruct := { ... } |
| 275 | routes.ServeJson(w, &mystruct) | 275 | routes.ServeJson(w, &mystruct) |
| 276 | } | 276 | } |
| 277 | ``` | 277 | ``` |
| 278 | Helper function for serving Xml, sets content type to application/xml: | 278 | Helper function for serving Xml, sets content type to application/xml: |
| 279 | ```go | 279 | ```go |
| 280 | func (this *AddController) Get() { | 280 | func (this *AddController) Get() { |
| 281 | mystruct := { ... } | 281 | mystruct := { ... } |
| 282 | routes.ServeXml(w, &mystruct) | 282 | routes.ServeXml(w, &mystruct) |
| 283 | } | 283 | } |
| 284 | ``` | 284 | ``` |
| 285 | 285 | ||
| 286 | ## Beego Variables | 286 | ## Beego Variables | ... | ... |
-
Please register or sign in to post a comment