美文网首页
使用Beego实现从访问url获取参数的方法

使用Beego实现从访问url获取参数的方法

作者: 程序员116号 | 来源:发表于2019-10-05 16:15 被阅读0次

1、 router.go 中设置正则路由

func init() {
    beego.Router("/article/?:id", &api.ArticleController{})
}

默认匹配 //例如对于URL”/article/123”可以匹配成功,此时变量”:id”值为”123”

2、controllers 中获取id

type ArticleController struct {
    beego.Controller
}

// 获取单个文章
func (this *ArticleController) Get() {
    id := this.Ctx.Input.Param(":id")
    log.Println(id)
}

调用this.Ctx.Input.Param()即可获取对应值

3、注意:beego默认开启模板解析

beego默认会开启模板解析,如果不关闭就会像这样:


在app.conf文件内添加上语句:autorender = false,此时得到了我们想要的结果。


相关文章

网友评论

      本文标题:使用Beego实现从访问url获取参数的方法

      本文链接:https://www.haomeiwen.com/subject/shgspctx.html