如“路由”一章所述, Iris 提供了一些处理器注册方法,每个方法都返回一个 Route 实例。

路由命名(Route naming)

路由命名非常简单,我们只需要调用返回的 *RouteName 字段来定义名字。

  1. package main
  2. import "github.com/kataras/iris/v12"
  3. func main() {
  4. app := iris.New()
  5. // define a function
  6. h := func(ctx iris.Context) {
  7. ctx.HTML("<b>Hi</b1>")
  8. }
  9. // handler registration and naming
  10. home := app.Get("/", h)
  11. home.Name = "home"
  12. // or
  13. app.Get("/about", h).Name = "about"
  14. app.Get("/page/{id}", h).Name = "page"g
  15. app.Run(iris.Addr(":8080"))
  16. }

从路由名字生成URL(Route reversing AKA generating URLs from the route name)

当我们为特定的路径注册处理器时,我们可以根据传递给 Iris 的结构化数据创建 URLs。如上面的例子所示,我们命名了三个路由,其中之一甚至带有参数。如果我们使用默认的 html/template 视图引擎,我们可以使用一个简单的操作来反转路由(生成示例的 URLs):

  1. Home: {{ urlpath "home" }}
  2. About: {{ urlpath "about" }}
  3. Page 17: {{ urlpath "page" "17" }}

上面的代理可以生成下面的输出:

  1. Home: http://localhost:8080/
  2. About: http://localhost:8080/about
  3. Page 17: http://localhost:8080/page/17

在代码中使用路由名字

我们可以使用以下方法/函数来处理命名路由(及其参数):

  • GetRoutes 函数获取所有注册的路由
  • GetRoute(routeName string) 方法通过名字获得路由
  • URL(routeName string, paramValues ...interface{}) 方法通过提供的值来生成 URL 字符串
  • Path(routeName string, paramValues ...interface{}) 方法通过提供的值生成URL的路径部分(没有主机地址和协议)。