Beego学习之路(一)
一、MVC
M=>Model:模型
V=>View:视图
C=>Control:控制器
分层原则:
客户端请求:http协议=>url,method,params;
服务器处理客户端过程:url=>handler(解析URL绑定)=>params(获得参数)=>db(操作数据库)=>template(解析模板)=>html;
客户端渲染:http渲染页面;
Mvc示意图
二、Beego的入门方法
1、网页静态获取参数
package main
import (
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
)
func main() {
//定义处理器函数
//路由url的绑定,一般可以与处理器函数同时写
beego.Get("/", func(ctx *context.Context) { //绑定路由
name := ctx.Input.Query("name") //查询name
ctx.Output.Context.WriteString("你输入的名字是:" + name) //将name响应到浏览器上
})
beego.Post("/", func(ctx *context.Context) { //使用POST方法接收对象
name := ctx.Input.Query("name")
ctx.Output.Context.WriteString("(POST)你输入的名字是" + name)
})
beego.Any("/any", func(ctx *context.Context) { //接收任意方法
name := ctx.Input.Query("name")
ctx.Output.Context.WriteString(fmt.Sprintf("(%s)你输入的名字是%s", ctx.Input.Method(), name))
})
//函数结构体,beego/context/Context
//以Get在这里插入代码片
beego.Run()
}
2、正则路由匹配网页
package main
import (
"fmt"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
)
func main() {
beego.Get("/id/:id(\\d+)/", func(ctx *context.Context) {
id := ctx.Input.Param("id") //获取id这个参数的值
ctx.WriteString(fmt.Sprintf("匹配到的id为%s", id)) //获取参数值
})
beego.Get("/name/:name(\\w+)/", func(ctx *context.Context) {
name := ctx.Input.Param("name")
ctx.WriteString(fmt.Sprintf("获取到的name为:%s", name))
})
beego.Get("/any/:context/", func(ctx *context.Context) {
id := ctx.Input.Param("id")
ctx.WriteString(id) //通用网址匹配到的id
})
beego.Get("/file/*", func(ctx *context.Context) { //文件使用splate获得
splat := ctx.Input.Param(":splat")
ctx.WriteString(fmt.Sprintf("匹配的file:%s", splat))
})
beego.Get("/ext/*.*", func(ctx *context.Context) {
path := ctx.Input.Param(":path") //获取文件的路径
ext := ctx.Input.Param(":ext") //获取文件的后缀
ctx.WriteString(fmt.Sprintf("文件的路径为%s", path))
ctx.WriteString(fmt.Sprintf("文件的路径为%s", ext))
})
beego.Run()
}
三、Restful风格
1、定义:是一种Api格式;
2、一切皆资源 url =>资源;
3、通过动作来表示对资源的操作类型
http method =>
Post =>创建/更新
Get =>获取/查询
Delete=>删除
Put =>更新
不同的方法来处理不同的业务逻辑
四、Restful风格控制器
package main
import (
"github.com/astaxie/beego"
)
type HomeController struct {
beego.Controller //继承/组合beego里面的Controller方法
}
func (this *HomeController) Get() {
this.Ctx.WriteString("This Method Is Get")
}
func (this *HomeController) Post() {
this.Ctx.WriteString("This Method Is Post")
}
func (this *HomeController) Delete() {
this.Ctx.WriteString("This Method Is Delete")
}
func main() {
beego.Router("/home/", &HomeController{}) //绑定路由
beego.Run()
}
同理也可以使用正则匹配方法
五、自定义路由规则控制
package main
import (
"fmt"
"github.com/astaxie/beego"
)
type TaskController struct {
beego.Controller //继承/组合beego里面的Controller方法
}
func (this *TaskController) Query() {
fmt.Println("Query")
this.Ctx.WriteString("Query")
}
func (this *TaskController) Add() {
fmt.Println("Add")
this.Ctx.WriteString("Add")
}
func main() {
beego.Router("/task/", &TaskController{}, "get,head:Query;post:Add") //绑定路由
beego.Run()
}
六、自动路由
package main
import (
"fmt"
"github.com/astaxie/beego"
)
type TaskController struct {
beego.Controller //继承/组合beego里面的Controller方法
}
func (c *TaskController) Query() {
fmt.Println("Query")
c.Ctx.WriteString("Query")
}
func (c *TaskController) Add() {
fmt.Println("Add")
c.Ctx.WriteString("Add")
}
func main() {
beego.AutoRouter(&TaskController{}) //自动路由,可以在URL中绑定方法
beego.Run()
}
请求
请求方法必须是小写
七、获取请求信息的几种方式
1、beego获取某个参数值的方法
package main
import (
"fmt"
"github.com/astaxie/beego"
)
type HomeController struct {
beego.Controller //组合
}
type InputForm struct {
Name string
}
func (c *HomeController) QueryParam() {
//第一种接收参数的方法
c.Ctx.Request.ParseForm() //解析
fmt.Println(c.Ctx.Request.Form)
//第二种接收参数的方法
fmt.Println(c.Ctx.Input.Query("name"))
//第三种接收参数的方法
var name string
c.Ctx.Input.Bind(&name, "name") //绑定参数到string
fmt.Println(name)
//第四种接收参数的方法,定义结构体
var form InputForm
fmt.Println(c.ParseForm(&form))
fmt.Printf("%#v", form)
c.Ctx.WriteString("")
}
func main() {
beego.AutoRouter(&HomeController{}) //绑定结构体
beego.Run()
}
2、上传文件
func (c *HomeController) File() {
c.SaveToFile("img", "./upload/a.jpg") //上传文件
}
func main() {
beego.AutoRouter(&HomeController{}) //绑定结构体
beego.Run()
}
3、json对象
(1)配置conf文件
(2)反序列化json对象
4、Cookie
(1)Cookie的获取
package main
import (
"fmt"
"github.com/astaxie/beego"
)
type HomeController struct {
beego.Controller //组合
}
func (c *HomeController) SetCookie() {
//方式一
cookie, err := c.Ctx.Request.Cookie("name")
fmt.Println(cookie, err)
//方式二:
fmt.Println(c.Ctx.Input.Cookie("name"))
c.Ctx.WriteString("")
//方式三:
fmt.Println(c.Ctx.GetCookie("name"))
}
func main() {
beego.AutoRouter(&HomeController{}) //绑定结构体
beego.Run()
}
(2)Cookie的设置
func (c *HomeController) SetCookie() {
//方式一
cookie, err := c.Ctx.Request.Cookie("name")
fmt.Println(cookie, err)
//方式二:
fmt.Println(c.Ctx.Input.Cookie("name"))
//方式三:
fmt.Println(c.Ctx.GetCookie("name"))
c.Ctx.SetCookie("name", "sad")
c.Ctx.WriteString("")
} //设置原始的cookie
func (c *HomeController) SecureCookie() {
var my string = "miyao"
c.Ctx.SetSecureCookie(my, "test", "vvvv") //设置加密cookie
fmt.Println(c.Ctx.GetSecureCookie(my, "test")) //解密
} //设置加密的cookie
八、获得响应头
九、响应到网页数据
十、Beego的模板技术
1、简单展示模板网页
package main
import (
"github.com/astaxie/beego"
)
type HomeController struct {
beego.Controller
}
func (c *HomeController) Index() {
c.TplName = "index.html"
}
func main() {
beego.AutoRouter(&HomeController{})
beego.Run()
}
2、向模板传输数据
(一)代码模板
package main
import (
"github.com/astaxie/beego"
)
type HomeController struct {
beego.Controller
}
func (c *HomeController) Index() {
c.Data["name"] = "This is my name"
c.Data["flag"] = false
c.Data["mapT"] = map[int]string{1: "测试1", 2: "测试2"}
c.TplName = "index.html"
}
func main() {
beego.AutoRouter(&HomeController{})
beego.Run()
}
(2)网页模板
{{ .name }}
<br/>
{{ if .flag }}
输出的是true
{{ else }}
输出的是false
{{ end }}
<br/>
{{ range .mapT }}
{{ . }}
{{ end }}
<br/>
{{ range $key,$value := .mapT }}
{{ $key }}={{ $value }}
{{ end }}
(3)自定义函数在模板调用
package main
import (
"github.com/astaxie/beego"
"strings"
)
type HomeController struct {
beego.Controller
}
func (c *HomeController) Index() {
c.Data["name"] = "ABCDEFG"
c.TplName = "index.html"
}
func main() {
beego.AddFuncMap("lower", func(in string) string {
return strings.ToLower(in)
})
beego.AutoRouter(&HomeController{})
beego.Run()
}
3、模板复用(即之前的模板嵌套)