mirror of
https://github.com/astaxie/beego.git
synced 2024-11-25 19:10:54 +00:00
parent
e81795a01b
commit
a7791c2786
14
README.md
14
README.md
@ -30,7 +30,7 @@ Here is the canonical "Hello, world" example app for beego:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (this *MainController) Get() {
|
func (this *MainController) Get() {
|
||||||
this.Ct.WriteString("hello world")
|
this.Ctx.WriteString("hello world")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -177,10 +177,10 @@ So you can define ChildStruct method to accomplish the interface's method, now l
|
|||||||
|
|
||||||
func (this *AddController) Post() {
|
func (this *AddController) Post() {
|
||||||
//data deal with
|
//data deal with
|
||||||
this.Ct.Request.ParseForm()
|
this.Ctx.Request.ParseForm()
|
||||||
pkgname := this.Ct.Request.Form.Get("pkgname")
|
pkgname := this.Ctx.Request.Form.Get("pkgname")
|
||||||
content := this.Ct.Request.Form.Get("content")
|
content := this.Ctx.Request.Form.Get("content")
|
||||||
beego.Info(this.Ct.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
|
||||||
@ -194,7 +194,7 @@ So you can define ChildStruct method to accomplish the interface's method, now l
|
|||||||
at.Pkgid = pk.Id
|
at.Pkgid = pk.Id
|
||||||
at.Content = content
|
at.Content = content
|
||||||
models.InsertArticle(at)
|
models.InsertArticle(at)
|
||||||
this.Ct.Redirect(302, "/admin/index")
|
this.Ctx.Redirect(302, "/admin/index")
|
||||||
}
|
}
|
||||||
|
|
||||||
## View / Template
|
## View / Template
|
||||||
@ -216,7 +216,7 @@ then beego will find the file in the path:`/views/admin/add.tpl`
|
|||||||
|
|
||||||
if you don't set TplNames,beego will find like this:
|
if you don't set TplNames,beego will find like this:
|
||||||
|
|
||||||
c.TplNames = c.ChildName + "/" + c.Ct.Request.Method + "." + c.TplExt
|
c.TplNames = c.ChildName + "/" + c.Ctx.Request.Method + "." + c.TplExt
|
||||||
|
|
||||||
So if the ChildName="AddController",Request Method= "POST",default TplEXT="tpl"
|
So if the ChildName="AddController",Request Method= "POST",default TplEXT="tpl"
|
||||||
So beego will file the file in the path:`/view/AddController/POST.tpl`
|
So beego will file the file in the path:`/view/AddController/POST.tpl`
|
||||||
|
@ -13,7 +13,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Controller struct {
|
type Controller struct {
|
||||||
Ct *Context
|
Ctx *Context
|
||||||
Tpl *template.Template
|
Tpl *template.Template
|
||||||
Data map[interface{}]interface{}
|
Data map[interface{}]interface{}
|
||||||
ChildName string
|
ChildName string
|
||||||
@ -36,14 +36,14 @@ type ControllerInterface interface {
|
|||||||
Render() error
|
Render() error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) Init(ct *Context, cn string) {
|
func (c *Controller) Init(ctx *Context, cn string) {
|
||||||
c.Data = make(map[interface{}]interface{})
|
c.Data = make(map[interface{}]interface{})
|
||||||
c.Tpl = template.New(cn + ct.Request.Method)
|
c.Tpl = template.New(cn + ctx.Request.Method)
|
||||||
c.Tpl = c.Tpl.Funcs(beegoTplFuncMap)
|
c.Tpl = c.Tpl.Funcs(beegoTplFuncMap)
|
||||||
c.Layout = ""
|
c.Layout = ""
|
||||||
c.TplNames = ""
|
c.TplNames = ""
|
||||||
c.ChildName = cn
|
c.ChildName = cn
|
||||||
c.Ct = ct
|
c.Ctx = ctx
|
||||||
c.TplExt = "tpl"
|
c.TplExt = "tpl"
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -57,38 +57,38 @@ func (c *Controller) Finish() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) Get() {
|
func (c *Controller) Get() {
|
||||||
http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405)
|
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) Post() {
|
func (c *Controller) Post() {
|
||||||
http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405)
|
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) Delete() {
|
func (c *Controller) Delete() {
|
||||||
http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405)
|
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) Put() {
|
func (c *Controller) Put() {
|
||||||
http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405)
|
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) Head() {
|
func (c *Controller) Head() {
|
||||||
http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405)
|
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) Patch() {
|
func (c *Controller) Patch() {
|
||||||
http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405)
|
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) Options() {
|
func (c *Controller) Options() {
|
||||||
http.Error(c.Ct.ResponseWriter, "Method Not Allowed", 405)
|
http.Error(c.Ctx.ResponseWriter, "Method Not Allowed", 405)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) Render() error {
|
func (c *Controller) Render() error {
|
||||||
//if the controller has set layout, then first get the tplname's content set the content to the layout
|
//if the controller has set layout, then first get the tplname's content set the content to the layout
|
||||||
if c.Layout != "" {
|
if c.Layout != "" {
|
||||||
if c.TplNames == "" {
|
if c.TplNames == "" {
|
||||||
c.TplNames = c.ChildName + "/" + c.Ct.Request.Method + "." + c.TplExt
|
c.TplNames = c.ChildName + "/" + c.Ctx.Request.Method + "." + c.TplExt
|
||||||
}
|
}
|
||||||
t, err := c.Tpl.ParseFiles(path.Join(ViewsPath, c.TplNames), path.Join(ViewsPath, c.Layout))
|
t, err := c.Tpl.ParseFiles(path.Join(ViewsPath, c.TplNames), path.Join(ViewsPath, c.Layout))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -100,20 +100,20 @@ func (c *Controller) Render() error {
|
|||||||
tplcontent, _ := ioutil.ReadAll(newbytes)
|
tplcontent, _ := ioutil.ReadAll(newbytes)
|
||||||
c.Data["LayoutContent"] = template.HTML(string(tplcontent))
|
c.Data["LayoutContent"] = template.HTML(string(tplcontent))
|
||||||
_, file = path.Split(c.Layout)
|
_, file = path.Split(c.Layout)
|
||||||
err = t.ExecuteTemplate(c.Ct.ResponseWriter, file, c.Data)
|
err = t.ExecuteTemplate(c.Ctx.ResponseWriter, file, c.Data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Trace("template Execute err:", err)
|
Trace("template Execute err:", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if c.TplNames == "" {
|
if c.TplNames == "" {
|
||||||
c.TplNames = c.ChildName + "/" + c.Ct.Request.Method + "." + c.TplExt
|
c.TplNames = c.ChildName + "/" + c.Ctx.Request.Method + "." + c.TplExt
|
||||||
}
|
}
|
||||||
t, err := c.Tpl.ParseFiles(path.Join(ViewsPath, c.TplNames))
|
t, err := c.Tpl.ParseFiles(path.Join(ViewsPath, c.TplNames))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Trace("template ParseFiles err:", err)
|
Trace("template ParseFiles err:", err)
|
||||||
}
|
}
|
||||||
_, file := path.Split(c.TplNames)
|
_, file := path.Split(c.TplNames)
|
||||||
err = t.ExecuteTemplate(c.Ct.ResponseWriter, file, c.Data)
|
err = t.ExecuteTemplate(c.Ctx.ResponseWriter, file, c.Data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Trace("template Execute err:", err)
|
Trace("template Execute err:", err)
|
||||||
}
|
}
|
||||||
@ -122,32 +122,32 @@ func (c *Controller) Render() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) Redirect(url string, code int) {
|
func (c *Controller) Redirect(url string, code int) {
|
||||||
c.Ct.Redirect(code, url)
|
c.Ctx.Redirect(code, url)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) ServeJson() {
|
func (c *Controller) ServeJson() {
|
||||||
content, err := json.MarshalIndent(c.Data, "", " ")
|
content, err := json.MarshalIndent(c.Data, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(c.Ct.ResponseWriter, err.Error(), http.StatusInternalServerError)
|
http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.Ct.SetHeader("Content-Length", strconv.Itoa(len(content)), true)
|
c.Ctx.SetHeader("Content-Length", strconv.Itoa(len(content)), true)
|
||||||
c.Ct.ContentType("json")
|
c.Ctx.ContentType("json")
|
||||||
c.Ct.ResponseWriter.Write(content)
|
c.Ctx.ResponseWriter.Write(content)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) ServeXml() {
|
func (c *Controller) ServeXml() {
|
||||||
content, err := xml.Marshal(c.Data)
|
content, err := xml.Marshal(c.Data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(c.Ct.ResponseWriter, err.Error(), http.StatusInternalServerError)
|
http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.Ct.SetHeader("Content-Length", strconv.Itoa(len(content)), true)
|
c.Ctx.SetHeader("Content-Length", strconv.Itoa(len(content)), true)
|
||||||
c.Ct.ContentType("xml")
|
c.Ctx.ContentType("xml")
|
||||||
c.Ct.ResponseWriter.Write(content)
|
c.Ctx.ResponseWriter.Write(content)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) Input() url.Values {
|
func (c *Controller) Input() url.Values {
|
||||||
c.Ct.Request.ParseForm()
|
c.Ctx.Request.ParseForm()
|
||||||
return c.Ct.Request.Form
|
return c.Ctx.Request.Form
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user