• Go Iris学习笔记01


    Iris MVC支持

    文档:

    支持所有 HTTP 方法, 例如,如果想要写一个 GET 那么在控制器中也要写一个 Get() 函数,你可以在一个控制器内定义多个函数。

    每个控制器通过 BeforeActivation 自定义事件回调,用来自定义控制器的结构的方法与自定义路径处理程序,如下:(还未实验)

    func (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
        b.Handle("GET", "/something/{id:long}", "MyCustomHandler", anyMiddleware...)
    }

    通过控制器方法的输入参数访问动态路径参数,不需要绑定。当你使用 iris 的默认语法来解析控制器处理程序时,你需要在方法后加上 "." 字符,大写字母是一个新的子路径。 官网例子:

     1  mvc.New(app.Party("/user")).Handle(new(user.Controller))
     2 
     3 func(*Controller) Get() - GET:/user.
     4 func(*Controller) Post() - POST:/user.
     5 func(*Controller) GetLogin() - GET:/user/login
     6 func(*Controller) PostLogin() - POST:/user/login
     7 func(*Controller) GetProfileFollowers() - GET:/user/profile/followers
     8 func(*Controller) PostProfileFollowers() - POST:/user/profile/followers
     9 func(*Controller) GetBy(id int64) - GET:/user/{param:long}
    10 func(*Controller) PostBy(id int64) - POST:/user/{param:long}
    mvc.New(app.Party("/profile")).Handle(new(profile.Controller))
    
    func(*Controller) GetBy(username string) - GET:/profile/{param:string}
    
    mvc.New(app.Party(
    "/assets")).Handle(new(file.Controller)) func(*Controller) GetByWildard(path string) - GET:/assets/{param:path} 方法函数接收器支持的类型: int,int64, boolstring

    测试demo

    main:

    package main
    
    import (
        "admin/web/controllers"
        "github.com/kataras/golog"
        "github.com/kataras/iris"
        "github.com/kataras/iris/middleware/logger"
        "github.com/kataras/iris/mvc"
    )
    
    func main()  {
        app := newApp()
        //app.RegisterView(iris.HTML("./web", ".html")) //加载模版文件
        app.StaticWeb("/static", "web/resources/static") // 设置静态资源,暂时没有
        app.RegisterView(iris.HTML("web/views", ".html").Reload(true))
        golog.Info() //暂时不知道干啥的
        app.Run(iris.Addr(":8081"))
    }
    
    func router(
    this *iris.Application){ //main := this.Party("/", crs).AllowMethods(iris.MethodOptions) //中间件 home:= this.Party("/") home.Get("/", func(ctx iris.Context) { // 首页模块 ctx.View("index/index.html") }) home.Get("/home", func(ctx iris.Context) { ctx.View("login/login.html") }) home.Get("/welcome", func(ctx iris.Context) { ctx.View("welcome/welcome.html") }) home.Get("/user/list/{page:int}",func(ctx iris.Context){ ctx.View("user/list.html") }) mvc.New(this.Party("/user")).Handle(new(controllers.UserController)) } func newApp() *iris.Application{ app := iris.New() preSettring(app) router(app) return app } func preSettring(app *iris.Application){ // 定义错误显示级别 app.Logger().SetLevel("debug") customLogger := logger.New(logger.Config{ //状态显示状态代码 Status: true, // IP显示请求的远程地址 IP: true, //方法显示http方法 Method: true, // Path显示请求路径 Path: true, // Query将url查询附加到Path。 Query: true, //Columns:true, // 如果不为空然后它的内容来自`ctx.Values(),Get("logger_message") //将添加到日志中。 MessageContextKeys: []string{"logger_message"}, //如果不为空然后它的内容来自`ctx.GetHeader(“User-Agent”) MessageHeaderKeys: []string{"User-Agent"}, }) app.Use( customLogger, //recover2.New(), ) }

    controller:

    package controllers
    
    import (
        "admin/models"
        "admin/services"
        "fmt"
    )
    
    type UserController struct {
        Service services.UserService
    }
    
    
    // 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
    func (c *UserController) Get() (result []models.User)  {
        fmt.Println("111111")
        //
        //data := c.Service.GetAll()
        //for k,_ := range data {
        //    result = append(result,models.User{1,string(k)})
        //}
        return
    }
    
    // 获取用户列表
    // curl -i http://localhost:8080/user/list
    func (u *UserController) GetList() (res string){
        fmt.Println("GetUserList")
        return "getUserlist"
    }
  • 相关阅读:
    JSON 基础完结 yz
    HTML5 基础教程一 yz
    HTML5 基础教程二 yz
    ADO.NET 之 一 yz
    [ lucene高级 ] Lucene docid,UID mapping and Payload [转]
    [ mongoDB ] Wordnik的MongoDB使用经验 [转]
    [ mongoDB ] MongoDB 客户端推荐
    [ mongoDB ] mongoDB replSet 添加节点注意事项
    Trie树实现[ java ]
    [ mongoDB ] MongoDB 连接池
  • 原文地址:https://www.cnblogs.com/lizhipengvvip/p/10797816.html
Copyright © 2020-2023  润新知