incomplete quick start
Showing
1 changed file
with
28 additions
and
22 deletions
| ... | @@ -3,10 +3,10 @@ Hey, you say you've never heard about Beego and don't know how to use it? Don't | ... | @@ -3,10 +3,10 @@ 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](#-5) |
| 11 | - [Filter and middleware](#-6) | 11 | - [Filter and middleware](#-6) |
| 12 | - [Controller](#-7) | 12 | - [Controller](#-7) |
| ... | @@ -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,39 +121,46 @@ In development mode, you have following effects: | ... | @@ -122,39 +121,46 @@ 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 |
| 162 | |||
| 163 | ## Static files | ||
| 158 | Go语言内部其实已经提供了`http.ServeFile`,通过这个函数可以实现静态文件的服务。beego针对这个功能进行了一层封装,通过下面的方式进行静态文件注册: | 164 | Go语言内部其实已经提供了`http.ServeFile`,通过这个函数可以实现静态文件的服务。beego针对这个功能进行了一层封装,通过下面的方式进行静态文件注册: |
| 159 | 165 | ||
| 160 | beego.SetStaticPath("/static","public") | 166 | beego.SetStaticPath("/static","public") | ... | ... |
-
Please register or sign in to post a comment