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, bool 和 string。
測試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" }
