1946e20e by Unknown

incomplete quick start-controller

1 parent 76472aaa
...@@ -7,9 +7,9 @@ Hey, you say you've never heard about Beego and don't know how to use it? Don't ...@@ -7,9 +7,9 @@ Hey, you say you've never heard about Beego and don't know how to use it? Don't
7 - [New project](#new-project) 7 - [New project](#new-project)
8 - [Development mode](#development-mode) 8 - [Development mode](#development-mode)
9 - [Router](#router) 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)
...@@ -160,26 +160,26 @@ For more convenient configure route rules, Beego references the idea from sinatr ...@@ -160,26 +160,26 @@ For more convenient configure route rules, Beego references the idea from sinatr
160 160
161 Match type string // match :hi is string type, Beego uses regular expression ([\w]+) automatically 161 Match type string // match :hi is string type, Beego uses regular expression ([\w]+) automatically
162 162
163 ## Static files 163 ##Static files
164 Go语言内部其实已经提供了`http.ServeFile`,通过这个函数可以实现静态文件的服务。beego针对这个功能进行了一层封装,通过下面的方式进行静态文件注册: 164 Go provides `http.ServeFile` for static files, Beego wrapped this function and use following way to register static file folder:
165 165
166 beego.SetStaticPath("/static","public") 166 beego.SetStaticPath("/static","public")
167 167
168 - 第一个参数是路径,url路径信息 168 - The first argument is the path of your URL.
169 - 第二个参数是静态文件目录(相对应用所在的目录) 169 - The second argument is the directory in your application path.
170 170
171 beego支持多个目录的静态文件注册,用户可以注册如下的静态文件目录: 171 Beego supports multiple static file directories as follows:
172 172
173 beego.SetStaticPath("/images","images") 173 beego.SetStaticPath("/images","images")
174 beego.SetStaticPath("/css","css") 174 beego.SetStaticPath("/css","css")
175 beego.SetStaticPath("/js","js") 175 beego.SetStaticPath("/js","js")
176 176
177 设置了如上的静态目录之后,用户访问`/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`.
178 178
179 ## 过滤和中间件 179 ##Filter and middleware
180 beego支持自定义过滤中间件,例如安全验证,强制跳转等 180 Beego supports customized filter and middleware, such as security verification, force redirect, etc.
181 181
182 如下例子所示,验证用户名是否是admin,应用于全部的请求: 182 Here is an example of verify user name of all requests, check if it's admin.
183 183
184 var FilterUser = func(w http.ResponseWriter, r *http.Request) { 184 var FilterUser = func(w http.ResponseWriter, r *http.Request) {
185 if r.URL.User == nil || r.URL.User.Username() != "admin" { 185 if r.URL.User == nil || r.URL.User.Username() != "admin" {
...@@ -189,73 +189,73 @@ beego支持自定义过滤中间件,例如安全验证,强制跳转等 ...@@ -189,73 +189,73 @@ beego支持自定义过滤中间件,例如安全验证,强制跳转等
189 189
190 beego.Filter(FilterUser) 190 beego.Filter(FilterUser)
191 191
192 还可以通过参数进行过滤,如果匹配参数就执行 192 You can also filter by arguments:
193 193
194 beego.Router("/:id([0-9]+)", &admin.EditController{}) 194 beego.Router("/:id([0-9]+)", &admin.EditController{})
195 beego.FilterParam("id", func(rw http.ResponseWriter, r *http.Request) { 195 beego.FilterParam("id", func(rw http.ResponseWriter, r *http.Request) {
196 dosomething() 196 dosomething()
197 }) 197 })
198 198
199 当然你还可以通过前缀过滤 199 Filter by prefix is also available:
200 200
201 beego.FilterPrefixPath("/admin", func(rw http.ResponseWriter, r *http.Request) { 201 beego.FilterPrefixPath("/admin", func(rw http.ResponseWriter, r *http.Request) {
202 dosomething() 202 dosomething()
203 }) 203 })
204 204
205 ## 控制器设计 205 ##Controller
206 基于beego的Controller设计,只需要匿名组合`beego.Controller`就可以了,如下所示: 206 Use `beego.controller` as anonymous in your controller struct to implement the interface in Beego:
207 207
208 type xxxController struct { 208 type xxxController struct {
209 beego.Controller 209 beego.Controller
210 } 210 }
211 211
212 `beego.Controller`实现了接口`beego.ControllerInterface``beego.ControllerInterface`定义了如下函数: 212 `beego.Controller` implemented`beego.ControllerInterface`, `beego.ControllerInterface` defined following methods:
213 213
214 - Init(ct *Context, cn string) 214 - Init(ct `*`Context, cn string)
215 215
216 这个函数主要初始化了Context、相应的Controller名称,模板名,初始化模板参数的容器Data 216 Initialize context, controller's name, template's name, and container of template arguments
217 217
218 - Prepare() 218 - Prepare()
219 219
220 这个函数主要是为了用户扩展用的,这个函数会在下面定义的这些Method方法之前执行,用户可以重写这个函数实现类似用户验证之类。 220 This is for expend usages, it executes before all the following methods. Users can overload this method for verification for example.
221 221
222 - Get() 222 - Get()
223 223
224 如果用户请求的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.
225 225
226 - Post() 226 - Post()
227 227
228 如果用户请求的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.
229 229
230 - Delete() 230 - Delete()
231 231
232 如果用户请求的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.
233 233
234 - Put() 234 - Put()
235 235
236 如果用户请求的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.
237 237
238 - Head() 238 - Head()
239 239
240 如果用户请求的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.
241 241
242 - Patch() 242 - Patch()
243 243
244 如果用户请求的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.
245 245
246 - Options() 246 - Options()
247 247
248 如果用户请求的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.
249 249
250 - Finish() 250 - Finish()
251 251
252 这个函数实在执行完相应的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.
253 253
254 - Render() error 254 - Render() error
255 255
256 这个函数主要用来实现渲染模板,如果beego.AutoRender为true的情况下才会执行。 256 This method is for rendering template, it executes automatically when you set beego.AutoRender to true.
257 257
258 所以通过子struct的方法重写,用户就可以实现自己的逻辑,接下来我们看一个实际的例子: 258 Overload all methods for all customized logic processes, let's see an example:
259 259
260 type AddController struct { 260 type AddController struct {
261 beego.Controller 261 beego.Controller
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!