Beego的自定义Controller
Jul 28, 2017 · 2 min read
beego的路由组件为Router,其功能函数为Router(path,controller),其中path为请求路径,controller则为该路径对应的controller实现。要实现自己的controller,可以通过实现beego的ControllerInterface接口,不过作为框架的使用者,我们可以更加便利的通过组合beego.Controller结构体,该结构体已经实现了ControllerInterface接口。所以实现自己的Controller,只需要复写想要重新实现的方法即可。
import "github.com/astaxie/beego"
type UserController struct {
beego.Controller
}
func (r *UserController) Get() {
r.Ctx.WriteString("hello")
}然后在程序启动时进行Router注册
beego.Router("/user",&UserController{})访问路径 /user
☁ ~ curl localhost:8080/user/
hello