e88c2be0 by astaxie

update docs & update beego's version

1 parent d5ddd0a9
...@@ -13,7 +13,7 @@ import ( ...@@ -13,7 +13,7 @@ import (
13 "time" 13 "time"
14 ) 14 )
15 15
16 const VERSION = "0.7.2" 16 const VERSION = "0.8.0"
17 17
18 var ( 18 var (
19 BeeApp *App 19 BeeApp *App
......
...@@ -4,25 +4,25 @@ ...@@ -4,25 +4,25 @@
4 4
5 **导航** 5 **导航**
6 6
7 - [最小应用](#-1) 7 - [最小应用](#%E6%9C%80%E5%B0%8F%E5%BA%94%E7%94%A8)
8 - [新建项目](#-2) 8 - [新建项目](#%E6%96%B0%E5%BB%BA%E9%A1%B9%E7%9B%AE)
9 - [开发模式](#-3) 9 - [开发模式](#%E5%BC%80%E5%8F%91%E6%A8%A1%E5%BC%8F)
10 - [路由设置](#-4) 10 - [路由设置](%E8%B7%AF%E7%94%B1%E8%AE%BE%E7%BD%AE)
11 - [静态文件](#-5) 11 - [静态文件](#%E9%9D%99%E6%80%81%E6%96%87%E4%BB%B6)
12 - [过滤和中间件](#-6) 12 - [过滤和中间件](#%E8%BF%87%E6%BB%A4%E5%92%8C%E4%B8%AD%E9%97%B4%E4%BB%B6)
13 - [Controller设计](#-7) 13 - [Controller设计](#%E6%8E%A7%E5%88%B6%E5%99%A8%E8%AE%BE%E8%AE%A1)
14 - [模板处理](#-8) 14 - [模板处理](#%E6%A8%A1%E6%9D%BF%E5%A4%84%E7%90%86)
15 - [request处理](#request) 15 - [request处理](#request%E5%A4%84%E7%90%86)
16 - [跳转和错误](#-15) 16 - [跳转和错误](#%E8%B7%B3%E8%BD%AC%E5%92%8C%E9%94%99%E8%AF%AF)
17 - [response处理](#response) 17 - [response处理](#response%E5%A4%84%E7%90%86)
18 - [Sessions](#sessions) 18 - [Sessions](#sessions)
19 - [Cache设置](#cache) 19 - [Cache设置](#cache%E8%AE%BE%E7%BD%AE)
20 - [安全的Map](#map) 20 - [安全的Map](#%E5%AE%89%E5%85%A8%E7%9A%84map)
21 - [日志处理](#-16) 21 - [日志处理](#%E6%97%A5%E5%BF%97%E5%A4%84%E7%90%86)
22 - [配置管理](#-17) 22 - [配置管理](#%E9%85%8D%E7%BD%AE%E7%AE%A1%E7%90%86)
23 - [beego参数](#-18) 23 - [beego参数](#%E7%B3%BB%E7%BB%9F%E9%BB%98%E8%AE%A4%E5%8F%82%E6%95%B0)
24 - [第三方应用集成](#-19) 24 - [第三方应用集成](#%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E9%9B%86%E6%88%90)
25 - [部署编译应用](#-20) 25 - [部署编译应用](#%E9%83%A8%E7%BD%B2%E7%BC%96%E8%AF%91%E5%BA%94%E7%94%A8)
26 26
27 27
28 ## 最小应用 28 ## 最小应用
...@@ -179,6 +179,37 @@ ...@@ -179,6 +179,37 @@
179 this.Ctx.Params[":path"] 179 this.Ctx.Params[":path"]
180 this.Ctx.Params[":ext"] 180 this.Ctx.Params[":ext"]
181 181
182 上面列举的是默认的请求方法名(请求的method和函数名一致,例如GET请求执行Get函数,POST请求执行Post函数),如果用户期望自定义函数名,那么可以使用如下方式:
183
184 beego.Router("/",&IndexController{},"*:Index")
185
186 使用第三个参数,第三个参数就是用来设置对应method到函数名,定义如下
187
188 - *表示任意的method都执行该函数
189 - 使用`httpmethod:funcname`格式来展示
190 - 多个不同的格式使用`;`分割
191 - 多个method对应同一个funcname,method之间通过`,`来分割
192
193 以下是一个RESTful的设计如下
194
195 - beego.Router("/api/list",&RestController{},"*:ListFood")
196 - beego.Router("/api/create",&RestController{},"post:CreateFood")
197 - beego.Router("/api/update",&RestController{},"put:UpdateFood")
198 - beego.Router("/api/delete",&RestController{},"delete:DeleteFood")
199
200 以下是多个http method指向同一个函数
201
202 beego.Router("/api",&RestController{},"get,post:ApiFunc")
203
204 一下是不同的method对应不同的函数,通过`;`进行分割
205
206 beego.Router("/simple",&SimpleController{},"get:GetFunc;post:PostFunc")
207
208 >>>如果同时存在*和对应的http method,那么优先执行http method的方法,例如同时注册了如下所示的路由:
209
210 >>> beego.Router("/simple",&SimpleController{},"*:AllFunc;post:PostFunc")
211
212 >>>那么执行POST请求的时候,执行PostFunc而不执行AllFunc
182 213
183 ## 静态文件 214 ## 静态文件
184 215
......
...@@ -434,6 +434,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -434,6 +434,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
434 if r.Method == "GET" { 434 if r.Method == "GET" {
435 if m, ok := runrouter.methods["get"]; ok { 435 if m, ok := runrouter.methods["get"]; ok {
436 method = vc.MethodByName(m) 436 method = vc.MethodByName(m)
437 } else if m, ok = runrouter.methods["*"]; ok {
438 method = vc.MethodByName(m)
437 } else { 439 } else {
438 method = vc.MethodByName("Get") 440 method = vc.MethodByName("Get")
439 } 441 }
...@@ -441,6 +443,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -441,6 +443,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
441 } else if r.Method == "HEAD" { 443 } else if r.Method == "HEAD" {
442 if m, ok := runrouter.methods["head"]; ok { 444 if m, ok := runrouter.methods["head"]; ok {
443 method = vc.MethodByName(m) 445 method = vc.MethodByName(m)
446 } else if m, ok = runrouter.methods["*"]; ok {
447 method = vc.MethodByName(m)
444 } else { 448 } else {
445 method = vc.MethodByName("Head") 449 method = vc.MethodByName("Head")
446 } 450 }
...@@ -448,6 +452,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -448,6 +452,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
448 } else if r.Method == "DELETE" || (r.Method == "POST" && r.Form.Get("_method") == "delete") { 452 } else if r.Method == "DELETE" || (r.Method == "POST" && r.Form.Get("_method") == "delete") {
449 if m, ok := runrouter.methods["delete"]; ok { 453 if m, ok := runrouter.methods["delete"]; ok {
450 method = vc.MethodByName(m) 454 method = vc.MethodByName(m)
455 } else if m, ok = runrouter.methods["*"]; ok {
456 method = vc.MethodByName(m)
451 } else { 457 } else {
452 method = vc.MethodByName("Delete") 458 method = vc.MethodByName("Delete")
453 } 459 }
...@@ -455,6 +461,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -455,6 +461,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
455 } else if r.Method == "PUT" || (r.Method == "POST" && r.Form.Get("_method") == "put") { 461 } else if r.Method == "PUT" || (r.Method == "POST" && r.Form.Get("_method") == "put") {
456 if m, ok := runrouter.methods["put"]; ok { 462 if m, ok := runrouter.methods["put"]; ok {
457 method = vc.MethodByName(m) 463 method = vc.MethodByName(m)
464 } else if m, ok = runrouter.methods["*"]; ok {
465 method = vc.MethodByName(m)
458 } else { 466 } else {
459 method = vc.MethodByName("Put") 467 method = vc.MethodByName("Put")
460 } 468 }
...@@ -462,6 +470,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -462,6 +470,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
462 } else if r.Method == "POST" { 470 } else if r.Method == "POST" {
463 if m, ok := runrouter.methods["post"]; ok { 471 if m, ok := runrouter.methods["post"]; ok {
464 method = vc.MethodByName(m) 472 method = vc.MethodByName(m)
473 } else if m, ok = runrouter.methods["*"]; ok {
474 method = vc.MethodByName(m)
465 } else { 475 } else {
466 method = vc.MethodByName("Post") 476 method = vc.MethodByName("Post")
467 } 477 }
...@@ -469,6 +479,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -469,6 +479,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
469 } else if r.Method == "PATCH" { 479 } else if r.Method == "PATCH" {
470 if m, ok := runrouter.methods["patch"]; ok { 480 if m, ok := runrouter.methods["patch"]; ok {
471 method = vc.MethodByName(m) 481 method = vc.MethodByName(m)
482 } else if m, ok = runrouter.methods["*"]; ok {
483 method = vc.MethodByName(m)
472 } else { 484 } else {
473 method = vc.MethodByName("Patch") 485 method = vc.MethodByName("Patch")
474 } 486 }
...@@ -476,6 +488,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -476,6 +488,8 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
476 } else if r.Method == "OPTIONS" { 488 } else if r.Method == "OPTIONS" {
477 if m, ok := runrouter.methods["options"]; ok { 489 if m, ok := runrouter.methods["options"]; ok {
478 method = vc.MethodByName(m) 490 method = vc.MethodByName(m)
491 } else if m, ok = runrouter.methods["*"]; ok {
492 method = vc.MethodByName(m)
479 } else { 493 } else {
480 method = vc.MethodByName("Options") 494 method = vc.MethodByName("Options")
481 } 495 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!