Reverse Lookups
As mentioned in the Routing chapter, Iris provides several handler registration methods, each of which returns a Route
instance.
Route naming
Route naming is easy, since we just call the returned *Route
with a Name
field to define a name:
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
view engine, we can use a simple action to reverse the routes (and generae actual URLs):
Above code would generate the following output:
Using route names in code
We can use the following methods/functions to work with named routes (and their parameters):
GetRoutes
function to get all registered routesGetRoute(routeName string)
method to retrieve a route by nameURL(routeName string, paramValues ...interface{})
method to generate url string based on supplied parametersPath(routeName string, paramValues ...interface{}
method to generate just the path (without host and protocol) portion of the URL based on provided values
Last updated