Iris MVC支持
文档:
支持所有 HTTP 方法, 例如,如果想要写一个 GET 那么在控制器中也要写一个 Get() 函数,你可以在一个控制器内定义多个函数。
每个控制器通过 BeforeActivation 自定义事件回调,用来自定义控制器的结构的方法与自定义路径处理程序,如下:(还未实验)
1func (m \*MyController) BeforeActivation(b mvc.BeforeActivation) { // b.Dependencies().Add/Remove // b.Router().Use/UseGlobal/Done // and any standard API call you already know // 1-> Method // 2-> Path // 3-> The controller's function name to be parsed as handler // 4-> Any handlers that should run before the MyCustomHandler 2 b.Handle("GET", "/something/{id:long}", "MyCustomHandler", anyMiddleware...) 3}
通过控制器方法的输入参数访问动态路径参数,不需要绑定。当你使用 iris 的默认语法来解析控制器处理程序时,你需要在方法后加上 "." 字符,大写字母是一个新的子路径。 官网例子:
1 1 mvc.New(app.Party("/user")).Handle(new(user.Controller)) 2 2 3 3 func(\*Controller) Get() - GET:/user. 4 4 func(\*Controller) Post() - POST:/user. 5 5 func(\*Controller) GetLogin() - GET:/user/login 6 6 func(\*Controller) PostLogin() - POST:/user/login 7 7 func(\*Controller) GetProfileFollowers() - GET:/user/profile/followers 8 8 func(\*Controller) PostProfileFollowers() - POST:/user/profile/followers 9 9 func(\*Controller) GetBy(id int64) - GET:/user/{param:long} 10 func(\*Controller) PostBy(id int64) - POST:/user/{param:long}
1mvc.New(app.Party("/profile")).Handle(new(profile.Controller)) 2 3func(\*Controller) GetBy(username string) - GET:/profile/{param:string} 4 5mvc.New(app.Party("/assets")).Handle(new(file.Controller)) 6 7func(\*Controller) GetByWildard(path string) - GET:/assets/{param:path} 8 9方法函数接收器支持的类型: int,int64, bool 和 string。
测试demo
main:
1package main 2 3import ( "admin/web/controllers" 4 "github.com/kataras/golog" 5 "github.com/kataras/iris" 6 "github.com/kataras/iris/middleware/logger" 7 "github.com/kataras/iris/mvc" ) 8 9func main() { 10 app :\= newApp() //app.RegisterView(iris.HTML("./web", ".html")) //加载模版文件 11 app.StaticWeb("/static", "web/resources/static") // 设置静态资源,暂时没有 12 app.RegisterView(iris.HTML("web/views", ".html").Reload(true)) 13 golog.Info() //暂时不知道干啥的 14 app.Run(iris.Addr(":8081")) 15} 16 17func router(this \*iris.Application){ //main := this.Party("/", crs).AllowMethods(iris.MethodOptions) //中间件 18 home:= this.Party("/") 19 home.Get("/", func(ctx iris.Context) { // 首页模块 20 ctx.View("index/index.html") 21 }) 22 home.Get("/home", func(ctx iris.Context) { 23 ctx.View("login/login.html") 24 }) 25 home.Get("/welcome", func(ctx iris.Context) { 26 ctx.View("welcome/welcome.html") 27 }) 28 29 home.Get("/user/list/{page:int}",func(ctx iris.Context){ 30 ctx.View("user/list.html") 31 }) 32 33 mvc.New(this.Party("/user")).Handle(new(controllers.UserController)) 34 35} 36func newApp() \*iris.Application{ 37 app :\= iris.New() 38 39 preSettring(app) 40 router(app) return app 41} 42 43func preSettring(app \*iris.Application){ // 定义错误显示级别 44 app.Logger().SetLevel("debug") 45 46 customLogger :\= logger.New(logger.Config{ //状态显示状态代码 47 Status: true, // IP显示请求的远程地址 48 IP: true, //方法显示http方法 49 Method: true, // Path显示请求路径 50 Path: true, // Query将url查询附加到Path。 51 Query: true, //Columns:true, // 如果不为空然后它的内容来自\`ctx.Values(),Get("logger\_message") //将添加到日志中。 52 MessageContextKeys: \[\]string{"logger\_message"}, //如果不为空然后它的内容来自\`ctx.GetHeader(“User-Agent”) 53 MessageHeaderKeys: \[\]string{"User-Agent"}, 54 }) 55 app.Use( 56 customLogger, //recover2.New(), 57 ) 58 59}
controller:
1package controllers 2 3import ( "admin/models" 4 "admin/services" 5 "fmt" ) 6 7type UserController struct { 8 Service services.UserService 9} // curl -i http://localhost:8080/movies // 如果您有敏感数据,这是正确的方法: // func (c \*MovieController) Get() (results \[\]viewmodels.Movie) { // data := c.Service.GetAll() // for \_, movie := range data { // results = append(results, viewmodels.Movie{movie}) // } // return // } // Get方法 // curl -i http://localhost:8080/user/list 10func (c \*UserController) Get() (result \[\]models.User) { 11 fmt.Println("111111") // 12 //data := c.Service.GetAll() //for k,\_ := range data { // result = append(result,models.User{1,string(k)}) //} 13 return } // 获取用户列表 // curl -i http://localhost:8080/user/list 14func (u \*UserController) GetList() (res string){ 15 fmt.Println("GetUserList") return "getUserlist" }
本文转自 https://www.cnblogs.com/lizhipengvvip/p/10797816.html,如有侵权,请联系删除。
