Merge pull request #44 from Unknwon/master
incomplete quick start-controller
Showing
1 changed file
with
57 additions
and
51 deletions
| ... | @@ -3,13 +3,13 @@ Hey, you say you've never heard about Beego and don't know how to use it? Don't | ... | @@ -3,13 +3,13 @@ Hey, you say you've never heard about Beego and don't know how to use it? Don't |
| 3 | 3 | ||
| 4 | **Navigation** | 4 | **Navigation** |
| 5 | 5 | ||
| 6 | - [Hello world](#-1) | 6 | - [Hello world](#hello-world) |
| 7 | - [New project](#-2) | 7 | - [New project](#new-project) |
| 8 | - [Development mode](#-3) | 8 | - [Development mode](#development-mode) |
| 9 | - [Router](#-4) | 9 | - [Router](#router) |
| 10 | - [Static files](#-5) | 10 | - [Static files](#static-files) |
| 11 | - [Filter and middleware](#-6) | 11 | - [Filter and middleware](#filter-and-middleware) |
| 12 | - [Controller](#-7) | 12 | - [Controller](#controller) |
| 13 | - [Template](#-8) | 13 | - [Template](#-8) |
| 14 | - [Handle request](#request) | 14 | - [Handle request](#request) |
| 15 | - [Redirect and error](#-15) | 15 | - [Redirect and error](#-15) |
| ... | @@ -105,8 +105,7 @@ In development mode, you have following effects: | ... | @@ -105,8 +105,7 @@ In development mode, you have following effects: |
| 105 |  | 105 |  |
| 106 | 106 | ||
| 107 | ## Router | 107 | ## Router |
| 108 | 108 | The main function of router is to connect request URL and handler. Beego wrapped `Controller`, so it connects request URL and `ControllerInterface`. The `ControllerInterface` has following methods: | |
| 109 | 路由的主要功能是实现从请求地址到实现方法,beego中封装了`Controller`,所以路由是从路径到`ControllerInterface`的过程,`ControllerInterface`的方法有如下: | ||
| 110 | 109 | ||
| 111 | type ControllerInterface interface { | 110 | type ControllerInterface interface { |
| 112 | Init(ct *Context, cn string) | 111 | Init(ct *Context, cn string) |
| ... | @@ -122,58 +121,65 @@ In development mode, you have following effects: | ... | @@ -122,58 +121,65 @@ In development mode, you have following effects: |
| 122 | Render() error | 121 | Render() error |
| 123 | } | 122 | } |
| 124 | 123 | ||
| 125 | 这些方法`beego.Controller`都已经实现了,所以只要用户定义struct的时候匿名包含就可以了。当然更灵活的方法就是用户可以去自定义类似的方法,然后实现自己的逻辑。 | 124 | `beego.Controller` implemented all of them, so you just use this struct as anonymous field in your controller struct. Of course you have to overload corresponding methods for more specific usages. |
| 126 | 125 | ||
| 127 | 用户可以通过如下的方式进行路由设置: | 126 | Users can use following ways to register route rules: |
| 128 | 127 | ||
| 129 | beego.Router("/", &controllers.MainController{}) | 128 | beego.Router("/", &controllers.MainController{}) |
| 130 | beego.Router("/admin", &admin.UserController{}) | 129 | beego.Router("/admin", &admin.UserController{}) |
| 131 | beego.Router("/admin/index", &admin.ArticleController{}) | 130 | beego.Router("/admin/index", &admin.ArticleController{}) |
| 132 | beego.Router("/admin/addpkg", &admin.AddController{}) | 131 | beego.Router("/admin/addpkg", &admin.AddController{}) |
| 133 | 132 | ||
| 134 | 为了用户更加方便的路由设置,beego参考了sinatra的路由实现,支持多种方式的路由: | 133 | For more convenient configure route rules, Beego references the idea from sinatra, so it supports more kinds of route rules as follows: |
| 135 | 134 | ||
| 136 | - beego.Router("/api/:id([0-9]+)", &controllers.RController{}) | 135 | - beego.Router("/api/:id([0-9]+)", &controllers.RController{}) |
| 137 | 自定义正则匹配 //匹配 /api/123 :id= 123 | ||
| 138 | 136 | ||
| 139 | - beego.Router("/news/:all", &controllers.RController{}) | 137 | Customized regular expression match // match /api/123 :id= 123 |
| 140 | 全匹配方式 //匹配 /news/path/to/123.html :all= path/to/123.html | 138 | |
| 139 | - beego.Router("/news/:all", &controllers.RController{}) | ||
| 140 | |||
| 141 | Match rest of all // match /news/path/to/123.html :all= path/to/123.html | ||
| 141 | 142 | ||
| 142 | - beego.Router("/user/:username([\w]+)", &controllers.RController{}) | 143 | - beego.Router("/user/:username([\w]+)", &controllers.RController{}) |
| 143 | 正则字符串匹配 //匹配 /user/astaxie :username = astaxie | 144 | |
| 145 | Regular expression // match /user/astaxie :username = astaxie | ||
| 144 | 146 | ||
| 145 | - beego.Router("/download/*.*", &controllers.RController{}) | 147 | - beego.Router("/download/`*`.`*`", &controllers.RController{}) |
| 146 | *匹配方式 //匹配 /download/file/api.xml :path= file/api :ext=xml | 148 | |
| 149 | Wildcard character // match /download/file/api.xml :path= file/api :ext=xml | ||
| 147 | 150 | ||
| 148 | - beego.Router("/download/ceshi/*", &controllers.RController{}) | 151 | - beego.Router("/download/ceshi/`*`", &controllers.RController{}) |
| 149 | *全匹配方式 //匹配 /download/ceshi/file/api.json :splat=file/api.json | 152 | |
| 153 | wildcard character match rest of all // match /download/ceshi/file/api.json :splat=file/api.json | ||
| 150 | 154 | ||
| 151 | - beego.Router("/:id:int", &controllers.RController{}) | 155 | - beego.Router("/:id:int", &controllers.RController{}) |
| 152 | int类型设置方式 //匹配 :id为int类型,框架帮你实现了正则([0-9]+) | 156 | |
| 157 | Match type int // match :id is int type, Beego uses regular expression ([0-9]+) automatically | ||
| 153 | 158 | ||
| 154 | - beego.Router("/:hi:string", &controllers.RController{}) | 159 | - beego.Router("/:hi:string", &controllers.RController{}) |
| 155 | string类型设置方式 //匹配 :hi为string类型。框架帮你实现了正则([\w]+) | ||
| 156 | 160 | ||
| 157 | ## 静态文件 | 161 | Match type string // match :hi is string type, Beego uses regular expression ([\w]+) automatically |
| 158 | Go语言内部其实已经提供了`http.ServeFile`,通过这个函数可以实现静态文件的服务。beego针对这个功能进行了一层封装,通过下面的方式进行静态文件注册: | 162 | |
| 163 | ##Static files | ||
| 164 | Go provides `http.ServeFile` for static files, Beego wrapped this function and use following way to register static file folder: | ||
| 159 | 165 | ||
| 160 | beego.SetStaticPath("/static","public") | 166 | beego.SetStaticPath("/static","public") |
| 161 | 167 | ||
| 162 | - 第一个参数是路径,url路径信息 | 168 | - The first argument is the path of your URL. |
| 163 | - 第二个参数是静态文件目录(相对应用所在的目录) | 169 | - The second argument is the directory in your application path. |
| 164 | 170 | ||
| 165 | beego支持多个目录的静态文件注册,用户可以注册如下的静态文件目录: | 171 | Beego supports multiple static file directories as follows: |
| 166 | 172 | ||
| 167 | beego.SetStaticPath("/images","images") | 173 | beego.SetStaticPath("/images","images") |
| 168 | beego.SetStaticPath("/css","css") | 174 | beego.SetStaticPath("/css","css") |
| 169 | beego.SetStaticPath("/js","js") | 175 | beego.SetStaticPath("/js","js") |
| 170 | 176 | ||
| 171 | 设置了如上的静态目录之后,用户访问`/images/login/login.png`,那么就会访问应用对应的目录下面的`images/login/login.png`文件。如果是访问`/static/img/logo.png`,那么就访问`public/img/logo.png`文件。 | 177 | After you setting static directory, when users visit `/images/login/login.png`,Beego accesses `images/login/login.png` in related to your application directory. One more example, if users visit `/static/img/logo.png`, Beego accesses file `public/img/logo.png`. |
| 172 | 178 | ||
| 173 | ## 过滤和中间件 | 179 | ##Filter and middleware |
| 174 | beego支持自定义过滤中间件,例如安全验证,强制跳转等 | 180 | Beego supports customized filter and middleware, such as security verification, force redirect, etc. |
| 175 | 181 | ||
| 176 | 如下例子所示,验证用户名是否是admin,应用于全部的请求: | 182 | Here is an example of verify user name of all requests, check if it's admin. |
| 177 | 183 | ||
| 178 | var FilterUser = func(w http.ResponseWriter, r *http.Request) { | 184 | var FilterUser = func(w http.ResponseWriter, r *http.Request) { |
| 179 | if r.URL.User == nil || r.URL.User.Username() != "admin" { | 185 | if r.URL.User == nil || r.URL.User.Username() != "admin" { |
| ... | @@ -183,73 +189,73 @@ beego支持自定义过滤中间件,例如安全验证,强制跳转等 | ... | @@ -183,73 +189,73 @@ beego支持自定义过滤中间件,例如安全验证,强制跳转等 |
| 183 | 189 | ||
| 184 | beego.Filter(FilterUser) | 190 | beego.Filter(FilterUser) |
| 185 | 191 | ||
| 186 | 还可以通过参数进行过滤,如果匹配参数就执行 | 192 | You can also filter by arguments: |
| 187 | 193 | ||
| 188 | beego.Router("/:id([0-9]+)", &admin.EditController{}) | 194 | beego.Router("/:id([0-9]+)", &admin.EditController{}) |
| 189 | beego.FilterParam("id", func(rw http.ResponseWriter, r *http.Request) { | 195 | beego.FilterParam("id", func(rw http.ResponseWriter, r *http.Request) { |
| 190 | dosomething() | 196 | dosomething() |
| 191 | }) | 197 | }) |
| 192 | 198 | ||
| 193 | 当然你还可以通过前缀过滤 | 199 | Filter by prefix is also available: |
| 194 | 200 | ||
| 195 | beego.FilterPrefixPath("/admin", func(rw http.ResponseWriter, r *http.Request) { | 201 | beego.FilterPrefixPath("/admin", func(rw http.ResponseWriter, r *http.Request) { |
| 196 | dosomething() | 202 | dosomething() |
| 197 | }) | 203 | }) |
| 198 | 204 | ||
| 199 | ## 控制器设计 | 205 | ##Controller |
| 200 | 基于beego的Controller设计,只需要匿名组合`beego.Controller`就可以了,如下所示: | 206 | Use `beego.controller` as anonymous in your controller struct to implement the interface in Beego: |
| 201 | 207 | ||
| 202 | type xxxController struct { | 208 | type xxxController struct { |
| 203 | beego.Controller | 209 | beego.Controller |
| 204 | } | 210 | } |
| 205 | 211 | ||
| 206 | `beego.Controller`实现了接口`beego.ControllerInterface`,`beego.ControllerInterface`定义了如下函数: | 212 | `beego.Controller` implemented`beego.ControllerInterface`, `beego.ControllerInterface` defined following methods: |
| 207 | 213 | ||
| 208 | - Init(ct *Context, cn string) | 214 | - Init(ct `*`Context, cn string) |
| 209 | 215 | ||
| 210 | 这个函数主要初始化了Context、相应的Controller名称,模板名,初始化模板参数的容器Data | 216 | Initialize context, controller's name, template's name, and container of template arguments |
| 211 | 217 | ||
| 212 | - Prepare() | 218 | - Prepare() |
| 213 | 219 | ||
| 214 | 这个函数主要是为了用户扩展用的,这个函数会在下面定义的这些Method方法之前执行,用户可以重写这个函数实现类似用户验证之类。 | 220 | This is for expend usages, it executes before all the following methods. Users can overload this method for verification for example. |
| 215 | 221 | ||
| 216 | - Get() | 222 | - Get() |
| 217 | 223 | ||
| 218 | 如果用户请求的HTTP Method是GET, 那么就执行该函数,默认是403,用户继承的子struct中可以实现了该方法以处理Get请求. | 224 | This method executes when client sends request as GET method, 403 as default status code. Users overload this method for customized handle process of GET method. |
| 219 | 225 | ||
| 220 | - Post() | 226 | - Post() |
| 221 | 227 | ||
| 222 | 如果用户请求的HTTP Method是POST, 那么就执行该函数,默认是403,用户继承的子struct中可以实现了该方法以处理Post请求. | 228 | This method executes when client sends request as POST method, 403 as default status code. Users overload this method for customized handle process of POST method. |
| 223 | 229 | ||
| 224 | - Delete() | 230 | - Delete() |
| 225 | 231 | ||
| 226 | 如果用户请求的HTTP Method是DELETE, 那么就执行该函数,默认是403,用户继承的子struct中可以实现了该方法以处理Delete请求. | 232 | This method executes when client sends request as DELETE method, 403 as default status code. Users overload this method for customized handle process of DELETE method. |
| 227 | 233 | ||
| 228 | - Put() | 234 | - Put() |
| 229 | 235 | ||
| 230 | 如果用户请求的HTTP Method是PUT, 那么就执行该函数,默认是403,用户继承的子struct中可以实现了该方法以处理Put请求. | 236 | This method executes when client sends request as PUT method, 403 as default status code. Users overload this method for customized handle process of PUT method. |
| 231 | 237 | ||
| 232 | - Head() | 238 | - Head() |
| 233 | 239 | ||
| 234 | 如果用户请求的HTTP Method是HEAD, 那么就执行该函数,默认是403,用户继承的子struct中可以实现了该方法以处理Head请求. | 240 | This method executes when client sends request as HEAD method, 403 as default status code. Users overload this method for customized handle process of HEAD method. |
| 235 | 241 | ||
| 236 | - Patch() | 242 | - Patch() |
| 237 | 243 | ||
| 238 | 如果用户请求的HTTP Method是PATCH, 那么就执行该函数,默认是403,用户继承的子struct中可以实现了该方法以处理Patch请求. | 244 | This method executes when client sends request as PATCH method, 403 as default status code. Users overload this method for customized handle process of PATCH method. |
| 239 | 245 | ||
| 240 | - Options() | 246 | - Options() |
| 241 | 247 | ||
| 242 | 如果用户请求的HTTP Method是OPTIONS, 那么就执行该函数,默认是403,用户继承的子struct中可以实现了该方法以处理Options请求. | 248 | This method executes when client sends request as OPTIONS method, 403 as default status code. Users overload this method for customized handle process of OPTIONS method. |
| 243 | 249 | ||
| 244 | - Finish() | 250 | - Finish() |
| 245 | 251 | ||
| 246 | 这个函数实在执行完相应的http Method方法之后执行的,默认是空,用户可以在子Strcut中重写这个函数,执行例如数据库关闭,清理数据之类的工作 | 252 | This method executes after corresponding method finished, empty as default. User overload this method for more usages like close database, clean data, etc. |
| 247 | 253 | ||
| 248 | - Render() error | 254 | - Render() error |
| 249 | 255 | ||
| 250 | 这个函数主要用来实现渲染模板,如果beego.AutoRender为true的情况下才会执行。 | 256 | This method is for rendering template, it executes automatically when you set beego.AutoRender to true. |
| 251 | 257 | ||
| 252 | 所以通过子struct的方法重写,用户就可以实现自己的逻辑,接下来我们看一个实际的例子: | 258 | Overload all methods for all customized logic processes, let's see an example: |
| 253 | 259 | ||
| 254 | type AddController struct { | 260 | type AddController struct { |
| 255 | beego.Controller | 261 | beego.Controller | ... | ... |
-
Please register or sign in to post a comment