💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
File: routing/reverse/main.go ~~~ package main import ( "github.com/kataras/iris" "github.com/kataras/iris/core/router" ) func main() { app := iris.New() // 需要在视图引擎外部需要时手动反向路由. // 你通常不需要它,因为 {{ urlpath "routename" "path" "values" "here"}} rv := router.NewRoutePathReverser(app) myroute := app.Get("/anything/{anythingparameter:path}", func(ctx iris.Context) { paramValue := ctx.Params().Get("anythingparameter") ctx.Writef("The path after /anything is: %s", paramValue) }) myroute.Name = "myroute" // 尽管iris的视图引擎具有{{ urlpath "routename" "path values"}} already. app.Get("/reverse_myroute", func(ctx iris.Context) { myrouteRequestPath := rv.Path(myroute.Name, "any/path") ctx.HTML("Should be <b>/anything/any/path</b>: " + myrouteRequestPath) }) // 执行一个路由,类似于重定向但没有重定向:) app.Get("/execute_myroute", func(ctx iris.Context) { ctx.Exec("GET", "/anything/any/path") // like it was called by the client. }) // http://localhost:8080/reverse_myroute // http://localhost:8080/execute_myroute // http://localhost:8080/anything/any/path/here // // 有关更多反向路由示例,请参阅view / template_html_4示例 // 使用反向路由器组件以及{{url}}和{{urlpath}}模板函数. app.Run(iris.Addr(":8080")) } ~~~