最近在看欧洲杯,没空写。今天周六恰好写一篇。html
接着上篇来写《和lock一块儿学beego 博客系统开发为例(三)》git
这篇主要完成如下事项:github
下篇咱们要完成下面三个任务:ajax
一、控制器的使用json
二、路由的使用session
1、控制器的使用学习
前面一直是以article表为例,控制器也是以article为例。this
beego通常都在controllers文件下面,你也能够在里面创建子文件夹,能够根据项目的须要来划分,不过在控制器文件中,包的名称要对应起文件夹名称。.net
在controllers创建article.gocode
在这个文件里,包含4个方法
一、添加博客
二、编辑博客
三、博客列表
四、博客详情
下面详细介绍这个4个方法
一、添加博客方法
package controllers import ( . "blog/models" "strconv" "github.com/astaxie/beego" "github.com/astaxie/beego/utils/pagination" ) //添加blog type AddArticleController struct { BaseController } func (this *AddArticleController) Get() { if !this.isLogin { this.Redirect("/login", 302) return } /*userLogin := this.GetSession("userLogin") if userLogin == nil { this.Redirect("/login", 302) return } */ var art Article art.Status = 1 this.Data["art"] = art this.TplName = "article-form.tpl" } func (this *AddArticleController) Post() { if !this.isLogin { this.Data["json"] = map[string]interface{}{"code": 0, "message": "请先登陆"} this.ServeJSON() return } title := this.GetString("title") content := this.GetString("content") keywords := this.GetString("keywords") uri := this.GetString("uri") summary := this.GetString("summary") author := this.GetString("author") if "" == title { this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写标题"} this.ServeJSON() return } if "" == content { this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写内容"} this.ServeJSON() return } var art Article art.Title = title art.Keywords = keywords art.Uri = uri art.Summary = summary art.Content = content art.Author = author id, err := AddArticle(art) if err == nil { this.Data["json"] = map[string]interface{}{"code": 1, "message": "博客添加成功", "id": id} } else { this.Data["json"] = map[string]interface{}{"code": 0, "message": "博客添加出错"} } this.ServeJSON() }
说明一下,定义了一个结构,addArticle,继承了baseController.这后面再介绍。beego里默认有Get,Post等方法,咱们在addArticle里重写了Get和Post方法,这个在URL直接访问的时候直接调用Get方法,在作添加操做的时候Post提交请求。固然在这里你也能够用其它名称,不过在路由器里要指定定义的方法,若是采用默认的,在路由器里不用指定方法。
二、编辑博客方法
编辑方法能够与添加方法合并,不过这里方便作学习介绍,单独出来讲一说,其它就是Post方法重写。
/修改blog type EditArticleController struct { BaseController } func (this *EditArticleController) Get() { if !this.isLogin { this.Data["json"] = map[string]interface{}{"code": 0, "message": "请先登陆"} this.ServeJSON() return } idstr := this.Ctx.Input.Param(":id") id, err := strconv.Atoi(idstr) art, err := GetArticle(id) if err != nil { this.Redirect("/404.html", 302) } //this.Data["json"] = map[string]interface{}{"code": 0, "message": err} //this.ServeJSON() this.Data["art"] = art this.TplName = "article-form.tpl" } func (this *EditArticleController) Post() { id, err := this.GetInt("id") title := this.GetString("title") content := this.GetString("content") keywords := this.GetString("keywords") uri := this.GetString("uri") summary := this.GetString("summary") author := this.GetString("author") status, _ := this.GetInt("status") if "" == title { this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写标题"} this.ServeJSON() } if "" == content { this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写内容"} this.ServeJSON() } _, errAttr := GetArticle(id) if errAttr != nil { this.Data["json"] = map[string]interface{}{"code": 0, "message": "博客不存在"} this.ServeJSON() } var art Article art.Title = title art.Keywords = keywords art.Uri = uri art.Summary = summary art.Content = content art.Author = author art.Status = status err = UpdateArticle(id, art) if err == nil { this.Data["json"] = map[string]interface{}{"code": 1, "message": "博客修改为功", "id": id} } else { this.Data["json"] = map[string]interface{}{"code": 0, "message": "博客修改出错"} } this.ServeJSON() }
说明:就是一Get,一个Post的重写,里面用ORM来操做和引用模型里定义的方法。这里说一下beego的json返回是:
this.Data["json"] = map[string]interface{}{"code": 1, "message": "message"}
this.ServeJSON()
三、博客列表
//列表 type ListArticleController struct { BaseController } func (this *ListArticleController) Get() { page, err1 := this.GetInt("p") title := this.GetString("title") keywords := this.GetString("keywords") status := this.GetString("status") if err1 != nil { page = 1 } offset, err2 := beego.AppConfig.Int("pageoffset") if err2 != nil { offset = 9 } condArr := make(map[string]string) condArr["title"] = title condArr["keywords"] = keywords if !this.isLogin { condArr["status"] = "1" } else { condArr["status"] = status } countArticle := CountArticle(condArr) paginator := pagination.SetPaginator(this.Ctx, offset, countArticle) _, _, art := ListArticle(condArr, page, offset) this.Data["paginator"] = paginator this.Data["art"] = art //userLogin := this.GetSession("userLogin") //this.Data["isLogin"] = userLogin //this.Data["isLogin"] = this.isLogin this.TplName = "article.tpl" }
说明:调用了文章模型里ListArticle的方法来分页,分页要引用beego里“github.com/astaxie/beego/utils/pagination”,
SetPaginator(当前内容,显示条数,总数)。模型里ListArticle引用ORM里采用Limit 10 Offset 10的,与Limit 0,10是不同的,要注意一下!
四、博客详情
详情不只要显示博客的内容,还要显示博客的评论。
//详情 type ShowArticleController struct { BaseController } func (this *ShowArticleController) Get() { idstr := this.Ctx.Input.Param(":id") id, err := strconv.Atoi(idstr) art, err := GetArticle(id) if err != nil { this.Redirect("/404.html", 302) } if !this.isLogin { if art.Status == 0 { this.Redirect("/404.html", 302) } } this.Data["art"] = art //评论分页 page, err1 := this.GetInt("p") if err1 != nil { page = 1 } offset, err2 := beego.AppConfig.Int("pageoffset") if err2 != nil { offset = 9 } condCom := make(map[string]string) condCom["article_id"] = idstr if !this.isLogin { condCom["status"] = "1" } countComment := CountComment(condCom) paginator := pagination.SetPaginator(this.Ctx, offset, countComment) _, _, coms := ListComment(condCom, page, offset) this.Data["paginator"] = paginator this.Data["coms"] = coms this.TplName = "article-detail.tpl" }
小结:到此控制器介绍完毕,是否是很简单,对正常的CRUD来讲,直接定义一个结构,再重写Get和Post方法,基本上是能够正常显示。
2、路由的使用
路由的具体使用,能够果看Beego有官网,这里介绍仅仅对博客的路由介绍
路由在routers下router.go下,在路由下,你首先得引用控制器,这样在init方法里才能够调用其方法
package routers import ( "blog/controllers" "github.com/astaxie/beego" ) func init() { beego.Router("/", &controllers.ListArticleController{}) beego.Router("/404.html", &controllers.BaseController{}, "*:Go404") beego.Router("/article", &controllers.ListArticleController{}) beego.Router("/article/:id", &controllers.ShowArticleController{}) beego.Router("/login", &controllers.LoginUserController{}) beego.Router("/logout", &controllers.LogoutUserController{}) beego.Router("/article/add", &controllers.AddArticleController{}) beego.Router("/article/edit/:id", &controllers.EditArticleController{}) beego.Router("/comment/add", &controllers.AddCommentController{}) beego.Router("/comment/edit/status", &controllers.EditCommentController{}) beego.Router("/album", &controllers.ListAlbumController{}) beego.Router("/album/upload", &controllers.UploadAlbumController{}) beego.Router("/album/edit", &controllers.EditAlbumController{}) beego.Router("/about", &controllers.AboutUserController{}) beego.Router("/uploadmulti", &controllers.UploadMultiController{}) beego.Router("/upload", &controllers.UploadController{}) //beego.Router("/article/ajax/add", &controllers.AddArticleController{}, "*:AddPost") //beego.Router("/article/add", &controllers.AddArticleController{}, "*:Add") }
一些格式能够参考上面写的,你们应该能够看明白。
是否是很简单,有些像diago的路由吧!
好了,今天就写到这里吧,有什么不明白的,能够联系我,下篇主要介绍:
一、模板的使用
二、基控制器BaseController的定义
三、session的使用