merger & fix
Showing
1 changed file
with
40 additions
and
38 deletions
| ... | @@ -13,12 +13,10 @@ To install: | ... | @@ -13,12 +13,10 @@ To install: |
| 13 | 13 | ||
| 14 | go get github.com/astaxie/beego | 14 | go get github.com/astaxie/beego |
| 15 | 15 | ||
| 16 | go version: go1 release | ||
| 17 | |||
| 18 | ## Quick Start | 16 | ## Quick Start |
| 19 | ============ | 17 | ============ |
| 20 | Here is the canonical "Hello, world" example app for beego: | 18 | Here is the canonical "Hello, world" example app for beego: |
| 21 | 19 | ```go | |
| 22 | package main | 20 | package main |
| 23 | 21 | ||
| 24 | import ( | 22 | import ( |
| ... | @@ -35,10 +33,10 @@ Here is the canonical "Hello, world" example app for beego: | ... | @@ -35,10 +33,10 @@ Here is the canonical "Hello, world" example app for beego: |
| 35 | 33 | ||
| 36 | func main() { | 34 | func main() { |
| 37 | beego.RegisterController("/", &MainController{}) | 35 | beego.RegisterController("/", &MainController{}) |
| 36 | //beego.HttpPort = 8080 // default | ||
| 38 | beego.Run() | 37 | beego.Run() |
| 39 | } | 38 | } |
| 40 | 39 | ||
| 41 | |||
| 42 | default port:8080 | 40 | default port:8080 |
| 43 | 41 | ||
| 44 | http get http://localhost:8080 | 42 | http get http://localhost:8080 |
| ... | @@ -49,37 +47,37 @@ default port:8080 | ... | @@ -49,37 +47,37 @@ default port:8080 |
| 49 | 47 | ||
| 50 | hello world | 48 | hello world |
| 51 | 49 | ||
| 52 | also i write an app based on beego. you can get the code from:[beepkg](https://github.com/astaxie/beepkg) | 50 | A more complete example use of beego exists here:[beepkg](https://github.com/astaxie/beepkg) |
| 53 | 51 | ||
| 54 | in other way i has writing a tool to quick create an app based on beego. you can get the tools from [bee](https://github.com/astaxie/bee) | 52 | Some associated tools for beego reside in:[bee](https://github.com/astaxie/bee) |
| 55 | 53 | ||
| 56 | ## Router | 54 | ## Router |
| 57 | ============ | 55 | ============ |
| 58 | In beego, a route is a struct paired with a URL-matching pattern. The strcut has many method with the same name of http method to server the http request. Each route is associated with a block: | 56 | 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. |
| 59 | 57 | ```go | |
| 60 | beego.RegisterController("/", &controllers.MainController{}) | 58 | beego.RegisterController("/", &controllers.MainController{}) |
| 61 | beego.RegisterController("/admin", &admin.UserController{}) | 59 | beego.RegisterController("/admin", &admin.UserController{}) |
| 62 | beego.RegisterController("/admin/index", &admin.ArticleController{}) | 60 | beego.RegisterController("/admin/index", &admin.ArticleController{}) |
| 63 | beego.RegisterController("/admin/addpkg", &admin.AddController{}) | 61 | beego.RegisterController("/admin/addpkg", &admin.AddController{}) |
| 64 | 62 | ``` | |
| 65 | You can specify custom regular expressions for routes: | 63 | You can specify custom regular expressions for routes: |
| 66 | 64 | ```go | |
| 67 | beego.RegisterController("/admin/editpkg/:id([0-9]+)", &admin.EditController{}) | 65 | beego.RegisterController("/admin/editpkg/:id([0-9]+)", &admin.EditController{}) |
| 68 | beego.RegisterController("/admin/delpkg/:id([0-9]+)", &admin.DelController{}) | 66 | beego.RegisterController("/admin/delpkg/:id([0-9]+)", &admin.DelController{}) |
| 69 | beego.RegisterController("/:pkg(.*)", &controllers.MainController{}) | 67 | beego.RegisterController("/:pkg(.*)", &controllers.MainController{}) |
| 70 | 68 | ``` | |
| 71 | You can also create routes for static files: | 69 | You can also create routes for static files: |
| 72 | 70 | ||
| 73 | beego.BeeApp.SetStaticPath("/static","/public") | 71 | beego.BeeApp.SetStaticPath("/static","/public") |
| 74 | 72 | ||
| 75 | this will serve any files in /static, including files in subdirectories. For example request `/static/logo.gif` or `/static/style/main.css` will server with the file in the path `/pulic/logo.gif` or `/public/style/main.css` | 73 | This will serve any files in /static, including files in subdirectories. For example request `/static/logo.gif` or `/static/style/main.css` will server with the file in the path `/pulic/logo.gif` or `/public/style/main.css` |
| 76 | 74 | ||
| 77 | ## Filters / Middleware | 75 | ## Filters / Middleware |
| 78 | ============ | 76 | ============ |
| 79 | You can apply filters to routes, which is useful for enforcing security, redirects, etc. | 77 | You can apply filters to routes, which is useful for enforcing security, redirects, etc. |
| 80 | 78 | ||
| 81 | You can, for example, filter all request to enforce some type of security: | 79 | You can, for example, filter all request to enforce some type of security: |
| 82 | 80 | ```go | |
| 83 | var FilterUser = func(w http.ResponseWriter, r *http.Request) { | 81 | var FilterUser = func(w http.ResponseWriter, r *http.Request) { |
| 84 | if r.URL.User == nil || r.URL.User.Username() != "admin" { | 82 | if r.URL.User == nil || r.URL.User.Username() != "admin" { |
| 85 | http.Error(w, "", http.StatusUnauthorized) | 83 | http.Error(w, "", http.StatusUnauthorized) |
| ... | @@ -87,30 +85,30 @@ You can, for example, filter all request to enforce some type of security: | ... | @@ -87,30 +85,30 @@ You can, for example, filter all request to enforce some type of security: |
| 87 | } | 85 | } |
| 88 | 86 | ||
| 89 | beego.Filter(FilterUser) | 87 | beego.Filter(FilterUser) |
| 90 | 88 | ``` | |
| 91 | You can also apply filters only when certain REST URL Parameters exist: | 89 | You can also apply filters only when certain REST URL Parameters exist: |
| 92 | 90 | ```go | |
| 93 | beego.RegisterController("/:id([0-9]+)", &admin.EditController{}) | 91 | beego.RegisterController("/:id([0-9]+)", &admin.EditController{}) |
| 94 | beego.FilterParam("id", func(rw http.ResponseWriter, r *http.Request) { | 92 | beego.FilterParam("id", func(rw http.ResponseWriter, r *http.Request) { |
| 95 | ... | 93 | ... |
| 96 | }) | 94 | }) |
| 97 | 95 | ``` | |
| 98 | also You can apply filters only when certain prefix URL path exist: | 96 | Additionally, You can apply filters only when certain prefix URL path exist: |
| 99 | 97 | ```go | |
| 100 | beego.FilterPrefixPath("/admin", func(rw http.ResponseWriter, r *http.Request) { | 98 | beego.FilterPrefixPath("/admin", func(rw http.ResponseWriter, r *http.Request) { |
| 101 | … auth | 99 | … auth |
| 102 | }) | 100 | }) |
| 103 | 101 | ``` | |
| 104 | 102 | ||
| 105 | ## Controller / Struct | 103 | ## Controller / Struct |
| 106 | ============ | 104 | ============ |
| 107 | you type a ChildStruct has anonymous type `beego.Controller` | 105 | To implement a beego Controller, embed the `beego.Controller` struct: |
| 108 | 106 | ```go | |
| 109 | type xxxController struct { | 107 | type xxxController struct { |
| 110 | beego.Controller | 108 | beego.Controller |
| 111 | } | 109 | } |
| 112 | 110 | ``` | |
| 113 | the `beego.Controller` is `beego.ControllerInterface` has the follow method: | 111 | `beego.Controller` satisfieds the `beego.ControllerInterface` interface, which defines the following methods: |
| 114 | 112 | ||
| 115 | - Init(ct *Context, cn string) | 113 | - Init(ct *Context, cn string) |
| 116 | 114 | ||
| ... | @@ -158,7 +156,7 @@ the `beego.Controller` is `beego.ControllerInterface` has the follow method: | ... | @@ -158,7 +156,7 @@ the `beego.Controller` is `beego.ControllerInterface` has the follow method: |
| 158 | 156 | ||
| 159 | 157 | ||
| 160 | So you can define ChildStruct method to accomplish the interface's method, now let us see an example: | 158 | So you can define ChildStruct method to accomplish the interface's method, now let us see an example: |
| 161 | 159 | ```go | |
| 162 | type AddController struct { | 160 | type AddController struct { |
| 163 | beego.Controller | 161 | beego.Controller |
| 164 | } | 162 | } |
| ... | @@ -193,12 +191,12 @@ So you can define ChildStruct method to accomplish the interface's method, now l | ... | @@ -193,12 +191,12 @@ So you can define ChildStruct method to accomplish the interface's method, now l |
| 193 | models.InsertArticle(at) | 191 | models.InsertArticle(at) |
| 194 | this.Ctx.Redirect(302, "/admin/index") | 192 | this.Ctx.Redirect(302, "/admin/index") |
| 195 | } | 193 | } |
| 196 | 194 | ``` | |
| 197 | ## View / Template | 195 | ## View / Template |
| 198 | ============ | 196 | ============ |
| 199 | ### template view path | 197 | ### template view path |
| 200 | 198 | ||
| 201 | the default viewPath is `/views`,you can put the template file in the views.beego will find the template from viewpath. | 199 | The default viewPath is `/views`, you can put the template file in the views. beego will find the template from viewpath. |
| 202 | 200 | ||
| 203 | also you can modify the viewpaths like this: | 201 | also you can modify the viewpaths like this: |
| 204 | 202 | ||
| ... | @@ -219,34 +217,36 @@ So if the ChildName="AddController",Request Method= "POST",default TplEXT="tpl" | ... | @@ -219,34 +217,36 @@ So if the ChildName="AddController",Request Method= "POST",default TplEXT="tpl" |
| 219 | So beego will file the file in the path:`/view/AddController/POST.tpl` | 217 | So beego will file the file in the path:`/view/AddController/POST.tpl` |
| 220 | 218 | ||
| 221 | ### autoRender | 219 | ### autoRender |
| 222 | In the controller you needn't to call render function. beego will auto call this function after HTTP' Method Call. | 220 | In the controller you needn't to call render function. beego will auto call this function after HTTP Method Call. |
| 223 | |||
| 224 | also you can close the autoRendder like this: | ||
| 225 | 221 | ||
| 222 | You can disable automatic invokation of autorender via the AutoRender Flag: | ||
| 223 | ```go | ||
| 226 | beego.AutoRender = false | 224 | beego.AutoRender = false |
| 227 | 225 | ``` | |
| 228 | 226 | ||
| 229 | ### layout | 227 | ### layout |
| 230 | beego also support layout. beego's layout is like this: | 228 | beego supports layouts for views. For example: |
| 231 | 229 | ```go | |
| 232 | this.Layout = "admin/layout.html" | 230 | this.Layout = "admin/layout.html" |
| 233 | this.TplNames = "admin/add.tpl" | 231 | this.TplNames = "admin/add.tpl" |
| 232 | ``` | ||
| 234 | 233 | ||
| 235 | in the layout.html you must define the variable like this to show sub template's content: | 234 | In layout.html you must define the variable like this to show sub template's content: |
| 236 | 235 | ||
| 237 | {{.LayoutContent}} | 236 | {{.LayoutContent}} |
| 238 | 237 | ||
| 239 | beego first Parse the file TplNames defined, then get the content from the sub template to the data["LayoutContent"], at last Parse the layout file and show it. | 238 | beego first parses the TplNames files, renders their content, and appends it to data["LayoutContent"]. |
| 240 | 239 | ||
| 241 | ### template function | 240 | ### template function |
| 242 | beego support users to define template function like this: | 241 | beego support users to define template function like this: |
| 243 | 242 | ```go | |
| 244 | func hello(in string)(out string){ | 243 | func hello(in string)(out string){ |
| 245 | out = in + "world" | 244 | out = in + "world" |
| 246 | return | 245 | return |
| 247 | } | 246 | } |
| 248 | 247 | ||
| 249 | beego.AddFuncMap("hi",hello) | 248 | beego.AddFuncMap("hi",hello) |
| 249 | ``` | ||
| 250 | 250 | ||
| 251 | then in you template you can use it like this: | 251 | then in you template you can use it like this: |
| 252 | 252 | ||
| ... | @@ -270,17 +270,20 @@ beego has three default defined funtion: | ... | @@ -270,17 +270,20 @@ beego has three default defined funtion: |
| 270 | You can use `beego.Controller.ServeJson` or `beego.Controller.ServeXml` for serializing to Json and Xml. I found myself constantly writing code to serialize, set content type, content length, etc. Feel free to use these functions to eliminate redundant code in your app. | 270 | You can use `beego.Controller.ServeJson` or `beego.Controller.ServeXml` for serializing to Json and Xml. I found myself constantly writing code to serialize, set content type, content length, etc. Feel free to use these functions to eliminate redundant code in your app. |
| 271 | 271 | ||
| 272 | Helper function for serving Json, sets content type to application/json: | 272 | Helper function for serving Json, sets content type to application/json: |
| 273 | 273 | ```go | |
| 274 | func (this *AddController) Get() { | 274 | func (this *AddController) Get() { |
| 275 | mystruct := { ... } | 275 | mystruct := { ... } |
| 276 | routes.ServeJson(w, &mystruct) | 276 | routes.ServeJson(w, &mystruct) |
| 277 | } | 277 | } |
| 278 | ``` | ||
| 278 | Helper function for serving Xml, sets content type to application/xml: | 279 | Helper function for serving Xml, sets content type to application/xml: |
| 279 | 280 | ```go | |
| 280 | func (this *AddController) Get() { | 281 | func (this *AddController) Get() { |
| 281 | mystruct := { ... } | 282 | mystruct := { ... } |
| 282 | routes.ServeXml(w, &mystruct) | 283 | routes.ServeXml(w, &mystruct) |
| 283 | } | 284 | } |
| 285 | ``` | ||
| 286 | |||
| 284 | ## Beego Variables | 287 | ## Beego Variables |
| 285 | ============ | 288 | ============ |
| 286 | beego has many default variables, as follow is a list to show: | 289 | beego has many default variables, as follow is a list to show: |
| ... | @@ -406,4 +409,3 @@ after set the log levels, in the logs function which below the setlevels willn't | ... | @@ -406,4 +409,3 @@ after set the log levels, in the logs function which below the setlevels willn't |
| 406 | after set levels to beego.LevelError | 409 | after set levels to beego.LevelError |
| 407 | 410 | ||
| 408 | Trace, Debug, Info, Warn will not output anything. So you can change it when in dev and prod mode. | 411 | Trace, Debug, Info, Warn will not output anything. So you can change it when in dev and prod mode. |
| 409 | |||
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or sign in to post a comment