mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 08:50:56 +00:00
add indent
This commit is contained in:
parent
6c89320183
commit
2417464c35
186
README.md
186
README.md
@ -17,25 +17,25 @@ To install:
|
|||||||
============
|
============
|
||||||
Here is the canonical "Hello, world" example app for beego:
|
Here is the canonical "Hello, world" example app for beego:
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/astaxie/beego"
|
"github.com/astaxie/beego"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MainController struct {
|
type MainController struct {
|
||||||
beego.Controller
|
beego.Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *MainController) Get() {
|
func (this *MainController) Get() {
|
||||||
this.Ctx.WriteString("hello world")
|
this.Ctx.WriteString("hello world")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
beego.RegisterController("/", &MainController{})
|
beego.RegisterController("/", &MainController{})
|
||||||
//beego.HttpPort = 8080 // default
|
//beego.HttpPort = 8080 // default
|
||||||
beego.Run()
|
beego.Run()
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
http get http://localhost:8080/
|
http get http://localhost:8080/
|
||||||
@ -54,16 +54,16 @@ Some associated tools for beego reside in:[bee](https://github.com/astaxie/bee)
|
|||||||
============
|
============
|
||||||
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.
|
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.
|
||||||
```go
|
```go
|
||||||
beego.RegisterController("/", &controllers.MainController{})
|
beego.RegisterController("/", &controllers.MainController{})
|
||||||
beego.RegisterController("/admin", &admin.UserController{})
|
beego.RegisterController("/admin", &admin.UserController{})
|
||||||
beego.RegisterController("/admin/index", &admin.ArticleController{})
|
beego.RegisterController("/admin/index", &admin.ArticleController{})
|
||||||
beego.RegisterController("/admin/addpkg", &admin.AddController{})
|
beego.RegisterController("/admin/addpkg", &admin.AddController{})
|
||||||
```
|
```
|
||||||
You can specify custom regular expressions for routes:
|
You can specify custom regular expressions for routes:
|
||||||
```go
|
```go
|
||||||
beego.RegisterController("/admin/editpkg/:id([0-9]+)", &admin.EditController{})
|
beego.RegisterController("/admin/editpkg/:id([0-9]+)", &admin.EditController{})
|
||||||
beego.RegisterController("/admin/delpkg/:id([0-9]+)", &admin.DelController{})
|
beego.RegisterController("/admin/delpkg/:id([0-9]+)", &admin.DelController{})
|
||||||
beego.RegisterController("/:pkg(.*)", &controllers.MainController{})
|
beego.RegisterController("/:pkg(.*)", &controllers.MainController{})
|
||||||
```
|
```
|
||||||
You can also create routes for static files:
|
You can also create routes for static files:
|
||||||
|
|
||||||
@ -77,35 +77,35 @@ You can apply filters to routes, which is useful for enforcing security, redirec
|
|||||||
|
|
||||||
You can, for example, filter all request to enforce some type of security:
|
You can, for example, filter all request to enforce some type of security:
|
||||||
```go
|
```go
|
||||||
var FilterUser = func(w http.ResponseWriter, r *http.Request) {
|
var FilterUser = func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.URL.User == nil || r.URL.User.Username() != "admin" {
|
if r.URL.User == nil || r.URL.User.Username() != "admin" {
|
||||||
http.Error(w, "", http.StatusUnauthorized)
|
http.Error(w, "", http.StatusUnauthorized)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
beego.Filter(FilterUser)
|
beego.Filter(FilterUser)
|
||||||
```
|
```
|
||||||
You can also apply filters only when certain REST URL Parameters exist:
|
You can also apply filters only when certain REST URL Parameters exist:
|
||||||
```go
|
```go
|
||||||
beego.RegisterController("/:id([0-9]+)", &admin.EditController{})
|
beego.RegisterController("/:id([0-9]+)", &admin.EditController{})
|
||||||
beego.FilterParam("id", func(rw http.ResponseWriter, r *http.Request) {
|
beego.FilterParam("id", func(rw http.ResponseWriter, r *http.Request) {
|
||||||
...
|
...
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
Additionally, You can apply filters only when certain prefix URL path exist:
|
Additionally, You can apply filters only when certain prefix URL path exist:
|
||||||
```go
|
```go
|
||||||
beego.FilterPrefixPath("/admin", func(rw http.ResponseWriter, r *http.Request) {
|
beego.FilterPrefixPath("/admin", func(rw http.ResponseWriter, r *http.Request) {
|
||||||
… auth
|
… auth
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
## Controller / Struct
|
## Controller / Struct
|
||||||
============
|
============
|
||||||
To implement a beego Controller, embed the `beego.Controller` struct:
|
To implement a beego Controller, embed the `beego.Controller` struct:
|
||||||
```go
|
```go
|
||||||
type xxxController struct {
|
type xxxController struct {
|
||||||
beego.Controller
|
beego.Controller
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
`beego.Controller` satisfieds the `beego.ControllerInterface` interface, which defines the following methods:
|
`beego.Controller` satisfieds the `beego.ControllerInterface` interface, which defines the following methods:
|
||||||
|
|
||||||
@ -156,40 +156,40 @@ To implement a beego Controller, embed the `beego.Controller` struct:
|
|||||||
|
|
||||||
So you can define ChildStruct method to accomplish the interface's method, now let us see an example:
|
So you can define ChildStruct method to accomplish the interface's method, now let us see an example:
|
||||||
```go
|
```go
|
||||||
type AddController struct {
|
type AddController struct {
|
||||||
beego.Controller
|
beego.Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *AddController) Prepare() {
|
func (this *AddController) Prepare() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *AddController) Get() {
|
func (this *AddController) Get() {
|
||||||
this.Layout = "admin/layout.html"
|
this.Layout = "admin/layout.html"
|
||||||
this.TplNames = "admin/add.tpl"
|
this.TplNames = "admin/add.tpl"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *AddController) Post() {
|
func (this *AddController) Post() {
|
||||||
//data deal with
|
//data deal with
|
||||||
this.Ctx.Request.ParseForm()
|
this.Ctx.Request.ParseForm()
|
||||||
pkgname := this.Ctx.Request.Form.Get("pkgname")
|
pkgname := this.Ctx.Request.Form.Get("pkgname")
|
||||||
content := this.Ctx.Request.Form.Get("content")
|
content := this.Ctx.Request.Form.Get("content")
|
||||||
beego.Info(this.Ctx.Request.Form)
|
beego.Info(this.Ctx.Request.Form)
|
||||||
pk := models.GetCruPkg(pkgname)
|
pk := models.GetCruPkg(pkgname)
|
||||||
if pk.Id == 0 {
|
if pk.Id == 0 {
|
||||||
var pp models.PkgEntity
|
var pp models.PkgEntity
|
||||||
pp.Pid = 0
|
pp.Pid = 0
|
||||||
pp.Pathname = pkgname
|
pp.Pathname = pkgname
|
||||||
pp.Intro = pkgname
|
pp.Intro = pkgname
|
||||||
models.InsertPkg(pp)
|
models.InsertPkg(pp)
|
||||||
pk = models.GetCruPkg(pkgname)
|
pk = models.GetCruPkg(pkgname)
|
||||||
}
|
|
||||||
var at models.Article
|
|
||||||
at.Pkgid = pk.Id
|
|
||||||
at.Content = content
|
|
||||||
models.InsertArticle(at)
|
|
||||||
this.Ctx.Redirect(302, "/admin/index")
|
|
||||||
}
|
}
|
||||||
|
var at models.Article
|
||||||
|
at.Pkgid = pk.Id
|
||||||
|
at.Content = content
|
||||||
|
models.InsertArticle(at)
|
||||||
|
this.Ctx.Redirect(302, "/admin/index")
|
||||||
|
}
|
||||||
```
|
```
|
||||||
## View / Template
|
## View / Template
|
||||||
============
|
============
|
||||||
@ -220,14 +220,14 @@ In the controller you needn't to call render function. beego will auto call this
|
|||||||
|
|
||||||
You can disable automatic invokation of autorender via the AutoRender Flag:
|
You can disable automatic invokation of autorender via the AutoRender Flag:
|
||||||
```go
|
```go
|
||||||
beego.AutoRender = false
|
beego.AutoRender = false
|
||||||
```
|
```
|
||||||
|
|
||||||
### layout
|
### layout
|
||||||
beego supports layouts for views. For example:
|
beego supports layouts for views. For example:
|
||||||
```go
|
```go
|
||||||
this.Layout = "admin/layout.html"
|
this.Layout = "admin/layout.html"
|
||||||
this.TplNames = "admin/add.tpl"
|
this.TplNames = "admin/add.tpl"
|
||||||
```
|
```
|
||||||
|
|
||||||
In layout.html you must define the variable like this to show sub template's content:
|
In layout.html you must define the variable like this to show sub template's content:
|
||||||
@ -239,12 +239,12 @@ beego first parses the TplNames files, renders their content, and appends it to
|
|||||||
### template function
|
### template function
|
||||||
beego support users to define template function like this:
|
beego support users to define template function like this:
|
||||||
```go
|
```go
|
||||||
func hello(in string)(out string){
|
func hello(in string)(out string){
|
||||||
out = in + "world"
|
out = in + "world"
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
beego.AddFuncMap("hi",hello)
|
beego.AddFuncMap("hi",hello)
|
||||||
```
|
```
|
||||||
|
|
||||||
then in you template you can use it like this:
|
then in you template you can use it like this:
|
||||||
@ -270,17 +270,17 @@ You can use `beego.Controller.ServeJson` or `beego.Controller.ServeXml` for seri
|
|||||||
|
|
||||||
Helper function for serving Json, sets content type to application/json:
|
Helper function for serving Json, sets content type to application/json:
|
||||||
```go
|
```go
|
||||||
func (this *AddController) Get() {
|
func (this *AddController) Get() {
|
||||||
mystruct := { ... }
|
mystruct := { ... }
|
||||||
routes.ServeJson(w, &mystruct)
|
routes.ServeJson(w, &mystruct)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
Helper function for serving Xml, sets content type to application/xml:
|
Helper function for serving Xml, sets content type to application/xml:
|
||||||
```go
|
```go
|
||||||
func (this *AddController) Get() {
|
func (this *AddController) Get() {
|
||||||
mystruct := { ... }
|
mystruct := { ... }
|
||||||
routes.ServeXml(w, &mystruct)
|
routes.ServeXml(w, &mystruct)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Beego Variables
|
## Beego Variables
|
||||||
|
Loading…
Reference in New Issue
Block a user