Routing

As mentioned in the Handlers chapter, Iris provides several handler registration methods, each of which returns a RouteNameFunc type.

Route naming

Route naming is easy, since we just call the returned RouteNameFunc with a string parameter to define a name:

package main

import (
    "github.com/kataras/iris"
)

func main() {

    // define a function
    render := func(ctx *iris.Context) {
        ctx.Render("index.html", nil)
    }

    // handler registration and naming
    iris.Get("/", render)("home")
    iris.Get("/about", render)("about")
    iris.Get("/page/:id", render)("page")

    iris.Listen(":8080")
}

Route reversing AKA generating URLs from the route name

When we register the handlers for a specific path, we get the ability to create URLs based on the structured data we pass to Iris. In the example above, we've named three routers, one of which even takes parameters. If we're using the default html/template templating engine, we can use a simple action to reverse the routes (and generae actual URLs):

Home: {{ url "home" }}
About: {{ url "about" }}
Page 17: {{ url "page" "17" }}

Above code would generate the following output:

Home: http://0.0.0.0:8080/ 
About: http://0.0.0.0:8080/about
Page 17: http://0.0.0.0:8080/page/17

Using route names in code

We can use the following methods/functions to work with named routes (and their parameters):

Examples

Check out the template_engines/template_html_4 example in the iris-contrib/examples repository.

results matching ""

    No results matching ""