路由写在一个文件里
首先确保 版本在0.5.0 或之上
go get -u github.com/shenyisyn/goft-gin@v0.5.0
分两步:
第一步,写一个config文件,如
type RouterConfig struct {
Goft *goft.Goft `inject:"-"`
IndexClass *classes.IndexClass `inject:"-"`
}
func NewRouterConfig() *RouterConfig {
return &RouterConfig{}
}
func (this *RouterConfig) IndexRoutes() interface{} {
this.Goft.Handle("GET", "/a", this.IndexClass.TestA)
this.Goft.Handle("GET", "/b", this.IndexClass.TestA)
this.Goft.Handle("GET", "/void", this.IndexClass.IndexVoid)
return goft.Empty
}
你可以把所有控制器都注入在RouterConfig中。然后在 下方的函数中,一次性写完
注册路由
goft.Ignite(cros(), errorFunc()).
Config(Configuration.NewMyConfig()).
Attach(fairing.NewGlobalFairing()).
Mount("", classes.NewIndexClass()).
Config(Configuration.NewRouterConfig()) //这句就是注册路由
强烈注意:注册路由的配置 必须写在控制器(Mount函数)下方
最后更新于
这有帮助吗?