beego学习笔记

网友投稿 668 2022-11-26

beego学习笔记

beego学习笔记

beego

bee脚手架工具

go get github.com/beego/bee

bee创建项目

// 将-好的二进制执行文件bee.exe路径添加到系统变量Path中// F:\all_go_project\goproject\binbee new beegodemo01 // 创建项目,默认会在c盘C:\Users\Administrator\下创建,可以把目录复制到别的地方进行开发,默认已经用go mod管理了

测试运行

bee run // 运行项目

目录介绍

├─conf //配置文件目录 -app.conf├─controllers //控制器目录,写逻辑代码的目录 -default.go├─models //模型字段目录├─routers // 路由url目录 -router.go├─static //静态资源目录 ├─css ├─img ├─js├─tests // 测试目录 -default_test.go├─views // 模板目录 -index.tpl

-

// ( "github.com/astaxie/beego" // 1.xx版本-方式,2.x版本-方式不同可参照github)>>>go run main.go

简单启动

package mainimport ( "github.com/astaxie/beego")func main(){ // 启动 服务 beego.Run()//默认监听8080端口}

调试模式

1.创建目录conf2.在conf目录下创建app.conf3.写入runmode=dev

路由

基本路由

package mainimport ( "github.com/astaxie/beego" "github.com/astaxie/beego/context" //使用的是beego里面的context而并非单独从context包)func main() { // 为根路径的Get方法绑定函数 beego.Get("/", func(ctx *context.Context) { // Output就相当于reponse响应,Body函数里面传递字节切片 ctx.Output.Body([]byte("hello world")) }) // post请求 beego.Post("/alice", func(ctx *context.Context) { // Output就相当于reponse响应 ctx.Output.Body([]byte("this is post function")) }) // Any请求,表示可以响应任何 beego.Any("/foo",func(ctx *context.Context){ ctx.Output.Body([]byte("bar")) }) //启动服务 beego.Run()}

正则路由

web.Router(“/api/?:id”, &controllers.RController{})默认匹配 //例如对于URL”/api/123”可以匹配成功,此时变量”:id”值为”123”,URL”/api/“可正常匹配web.Router(“/api/:id”, &controllers.RController{})默认匹配 //例如对于URL”/api/123”可以匹配成功,此时变量”:id”值为”123”,但URL”/api/“匹配失败web.Router(“/api/:id([0-9]+)“, &controllers.RController{})自定义正则匹配 //例如对于URL”/api/123”可以匹配成功,此时变量”:id”值为”123”web.Router(“/user/:username([\\w]+)“, &controllers.RController{})正则字符串匹配 //例如对于URL”/user/astaxie”可以匹配成功,此时变量”:username”值为”astaxie”web.Router(“/download/*.*”, &controllers.RController{})*匹配方式 //例如对于URL”/download/file/api.xml”可以匹配成功,此时变量”:path”值为”file/api”, “:ext”值为”xml” */web.Router(“/download/ceshi/*“, &controllers.RController{})*全匹配方式 //例如对于URL”/download/ceshi/file/api.json”可以匹配成功,此时变量”:splat”值为”file/api.json” */web.Router(“/:id:int”, &controllers.RController{})int 类型设置方式,匹配 :id为int 类型,框架帮你实现了正则 ([0-9]+)web.Router(“/:hi:string”, &controllers.RController{})string 类型设置方式,匹配 :hi 为 string 类型。框架帮你实现了正则 ([\w]+)web.Router(“/cms_:id([0-9]+).html”, &controllers.CmsController{})带有前缀的自定义正则 //匹配 :id 为正则类型。匹配 cms_123.html 这样的 url :id = 123// 示例package mainimport ( "github.com/astaxie/beego" "github.com/astaxie/beego/context" //使用的是beego里面的context而并非单独从context包)func main() { // Any请求,表示可以响应任何 // :id 匹配 123 beego.Any("/foo/:id/",func(ctx *context.Context){ ctx.Output.Body([]byte("this is id")) }) //启动服务 beego.Run()}>>> mainimport ( "github.com/astaxie/beego" "github.com/astaxie/beego/context" //使用的是beego里面的context而并非单独从context包)func main() { // Any请求,表示可以响应任何 // :id 匹配 "123" beego.Any("/foo/:id/",func(ctx *context.Context){ //获取路由参数 fmt.Println(ctx.Input.Param(":id")) // "123" ctx.Output.Body([]byte("this is id")) }) //启动服务 beego.Run()}

控制器

控制器介绍

// 基于 beego 的 Controller 设计,只需要匿名组合 beego.Controller 就可以了,如下所示:package mainimport ( "github.com/astaxie/beego")type xxxController struct{ beego.Controller // 嵌套beego的Controller,就拥有了beego.Controller的所有方法}

控制器方法

package mainimport ( "github.com/astaxie/beego")type HomeController struct{ beego.Controller}// 上面我们嵌套了beego.Controller,所以我们就可以重写它里面的get等方法func (c *HomeController) Get(){ }

路由控制器绑定

package mainimport ( "github.com/astaxie/beego" "github.com/astaxie/beego/context")type HomeController struct{ beego.Controller}// 定义方法func (c *HomeController) Get(){ // 方式1不使用模板,直接返回字符串 c.Ctx.WriteString("hello") // 返回字节切片 c.Ctx.Output.Body([]byte("HomeController.Get")) // 方式2使用渲染模板,如果用户不设置该参数,那么默认会去到模板目录的 Controller/<方法名>.tpl 查找,例如上面的方法会去 homecontroller/get.tpl(文件、文件夹必须小写) c.TplName = "index.tpl"}func main(){ // /home/ 路由地址 // &HomeController{} 结构体实例 beego.Router("/home/", &HomeController{})}

路由参数控制器绑定

package mainimport ( "github.com/astaxie/beego" "github.com/astaxie/beego/context")type UserController struct{ beego.Controller}func (c *UserController) Get(){ c.Ctx.Output.Body([]byte(c.Ctx.Input.Param(":id")))}func main(){ // /user/123/ beego.Router("/user/:id/", &UserController{}) // 正则路由需要用反引号 beego.Router(`/muser/:id(\d+)/`, &UserController{})}

自定义方法

// 上面列举的是默认的请求方法名(请求的 method 和函数名一致,例如 GET 请求执行 Get 函数,POST 请求执行 Post 函数),如果用户期望自定义函数名,那么可以使用如下方式beego.Router("/",&IndexController{},"*:Index") // *表示所有方法// 示例package mainimport ( "github.com/astaxie/beego" "github.com/astaxie/beego/context")type UserController struct{ beego.Controller}// 当get请求时访问Detail方法func (c *UserController) Detail(){ c.Ctx.Output.Body([]byte("this is detail function"))}// get和post请求时访问ApiFunc方法func (c *UserController) ApiFunc(){ c.Ctx.Output.Body([]byte("this is ApiFunc function"))}func main(){ // 参数3的位置表示指定请求方法对应的要执行的方法名 beego.Router("/user/", &UserController{}, "get:Detail") // 多个请求方法指向同一个要执行的方法 beego.Router("/user/", &UserController{},"get,post:ApiFunc") //不同的请求对应不同的方法,通过 ; 进行分割 beego.Router("/api/food/",&RestController{}, "get:ListFood;post:CreateFood;put:UpdateFood;delete:DeleteFood")}//注意事项如果你自定义了get请求对应的Detail方法以及post请求的Create方法,put和delete请求还使用原来的Put和Delete方法,那么在路由控制器中不仅要指定你自定义请求对应的方法,也需要将原来的方法隐射指定上,如下beego.Router("/api/food/",&RestController{}, "get:Deatil;post:Create;put:Put;delete:Delete")

自动路由(跟请求方法无关,只跟url有关)

请求数据处理

获取请求控制器和动作

package mainimport ( "github.com/astaxie/beego" "github.com/astaxie/beego/context")type RequestController struct{ beego.Controller}func (c *RequestController) Header(){ // 获取请求控制器和动作 controllerName, actionName := c.GetControllerAndAction() fmt.Println(controllerName, actionName) // RequestController, Header c.Ctx.Output.Body([]byte("header"))}func main(){ beego.AutoRouter(&RequestController{}) beego.Run()}

获取请求头信息

package mainimport ( "github.com/astaxie/beego" "github.com/astaxie/beego/context")type RequestController struct{ beego.Controller}func (c *RequestController) Header(){ ctx := c.Ctx input := ctx.Input //获取请求方法 fmt.Println(input.Method()) //请求协议。uri,url fmt.Println(input.Protocol(), input.URI(), input.URL()) //获取头信息中的user-agent fmt.Println(input.Header("User-Agent")) c.Ctx.Output.Body([]byte("header"))}func main(){ beego.AutoRouter(&RequestController{}) beego.Run()}

获取请求数据(查询字符串,body数据)

package mainimport ( "github.com/astaxie/beego" "github.com/astaxie/beego/context")type RequestController struct{ beego.Controller}func (c *RequestController) Header(){ ctx := c.Ctx input := ctx.Input // url查询字符串和body中数据都可以获取 input.Query("id") c.Ctx.Output.Body([]byte("header"))}func main(){ beego.AutoRouter(&RequestController{}) beego.Run()}

Controller获取请求数据

package mainimport ( "github.com/astaxie/beego" "github.com/astaxie/beego/context")type RequestController struct{ beego.Controller}func (c *RequestController) Header(){ // 获取id的数据 fmt.Println(c.GetInt("id")) c.GetString("name") c.GetStrings("hobby")// 返回字符串数组切片 c.GetBool("sex")// 返回bool和error c.GetFloat("money")// 返回float64和error c.Input() // 获取全部数据 c.Ctx.Output.Body([]byte("header"))}func main(){ beego.AutoRouter(&RequestController{}) beego.Run()}

解析到struct

//如果要把表单里的内容赋值到一个 struct 里,除了用上面的方法一个一个获取再赋值外,beego 提供了通过另外一个更便捷的方式,就是通过 struct 的字段名或 tag 与表单字段对应直接解析到 structpackage mainimport ( "github.com/astaxie/beego" "github.com/astaxie/beego/context")type user struct { Id int `form:"-"` Name interface{} `form:"username"` Age int `form:"age"` Email string}type LoginController struct{ beego.Controller}/*html表单名字:年龄:邮箱:*/func (c *LoginController) Post() { u := user{} if err := c.ParseForm(&u); err != nil { //handle error fmt.Println(error) } fmt.Println(u)}// 注意事项1.StructTag form 的定义和 renderform方法 共用一个标签2.定义 struct 时,字段名后如果有 form 这个 tag,则会以把 form 表单里的 name 和 tag 的名称一样的字段赋值给这个字段,否则就会把 form 表单里与字段名一样的表单内容赋值给这个字段。如上面例子中,会把表单中的 username 和 age 分别赋值给 user 里的 Name 和 Age 字段,而 Email 里的内容则会赋给 Email 这个字段。3.调用 Controller ParseForm 这个方法的时候,传入的参数必须为一个 struct 的指针,否则对 struct 的赋值不会成功并返回 xx must be a struct pointer 的错误。4.如果要忽略一个字段,有两种办法,一是:字段名小写开头,二是:form 标签的值设置为 -

获取requets body中数据

/*1.在配置文件app.conf里设置 copyrequestbody = true*/package mainimport ( "github.com/astaxie/beego" "github.com/astaxie/beego/context")type RequestController struct{ beego.Controller}func (c *RequestController) Header(){ input := ctx.Input fmt.Println(input.CopyBody(1024 * 1024)) fmt.Println(string(input.RequestBody)) c.Ctx.Output.Body([]byte("header"))}func main(){ beego.AutoRouter(&RequestController{}) beego.Run()}

文件上传

func (c *FormController) Post() { f, h, err := c.GetFile("uploadname") if err != nil { log.Fatal("getfile err ", err) } defer f.Close() c.SaveToFile("uploadname", "static/upload/" + h.Filename) // 保存位置在 static/upload, 没有文件夹要先创建 }

响应数据

TplName

package mainimport ( "github.com/astaxie/beego" "github.com/astaxie/beego/context")type ReponseController struct{ beego.Controller}func (c *ReponseController) Test(){ //不指定默认会去找views/responsecontroller/test.tpl c.TplName = "responsecontroller/test.html"}func main(){ beego.AutoRouter(&ReponseController{}) beego.Run()}

模板数据返回

package mainimport ( "github.com/astaxie/beego" "github.com/astaxie/beego/context")type ReponseController struct{ beego.Controller}func (c *ReponseController) Test(){ // 模板数据返回 c.Data["name"] = "kk" //不指定默认会去找views/responsecontroller/test.tpl c.TplName = "responsecontroller/test.html"}func main(){ beego.AutoRouter(&ReponseController{}) beego.Run()}

返回json数据

//返回json数据,需要把数据放在结构体中type User struct{ //form表单解析的时候用的tag,json解析的时候用的tag Username string `form:"username" json:"username"` Password string `form:"password" json:"password"` Hobby []string `form:"hobby" json:"hobby"`}func (c *UserController) GetUser(){ u := User{ Username: "张三", Password: "123456", Hobby: []string{"1", "2"} } //返回json数据 c.Data["json"] = u c.ServerJSON()}

路由跳转redirect

//方式1c.Redirect("/article/xml", 302)return//方式2c.Ctx.Redirect(302, "/article/xml")return

Session

配置开启

// 需要在配置文件中开启sessionon = true|false

操作session

// 都是通过contorller对象来操作// 设置c.SetSession(name string, value interface{})// 获取c.GetSession(name string) interface{}// 删除c.DelSession(name string)// 销毁c.DestroySession()

-------------------------------------------

个性签名:代码过万,键盘敲烂!!!

如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Springboot导入本地jar后 打包依赖无法加入的解决方案
下一篇:QRFC的几个demo程序
相关文章

 发表评论

暂时没有评论,来抢沙发吧~