Iris
Getting Started
  • What is Iris
  • 📌Getting started
    • Installation
    • Quick start
  • 🔌Routing
    • Middleware
    • API Versioning
  • 🗜️Compression
    • Index
  • ✈️Redirect
    • Redirect from Context
    • Rewrite Middleware
    • Multi Application Instances
  • 🖼️ View/Templates
    • Documentation
    • Benchmarks
    • ➲ Examples
  • 📁File Server
    • Introduction
    • Listing
    • In-memory Cache
    • HTTP/2 Push + Embedded + Cache and Compression
    • The PrefixDir function
    • Serve files from Context
    • ➲ Examples
  • 🌎Localization
    • Documentation
    • Sitemap
    • ➲ Examples
  • 🛡️Security
    • Basic Authentication
    • CORS
    • Sessions & Cookies
    • CSRF
    • JSON Web Tokens
    • Access Control
    • Anti-bot CAPTCHA
    • ➲ Examples
  • 🚀Responses
    • Text
    • HTML
    • Markdown
    • XML
    • YAML
    • Binary
    • JSON
    • JSONP
    • Problem
    • Protocol Buffers
    • MessagePack
    • Gzip
    • Content Negotiation
    • Stream
    • Server-Sent Events
    • HTTP/2 Push
    • Recorder
    • Outroduction
    • ➲ Examples
  • 📥Requests
    • URL Query
    • Headers
    • URL Path Parameters
    • Form
    • Text
    • XML
    • YAML
    • Binary
    • JSON
    • Validation
    • Protocol Buffers
    • MessagePack
    • Gzip
    • ➲ Examples
  • 💉Dependency Injection
    • Documentation
    • Register Dependency from Context
    • Inputs
    • Outputs
    • ➲ Examples
  • 🦏MVC
    • Quick start
    • Documentation
    • Handle Errors
    • Sessions
    • Websockets
    • gRPC
    • ➲ Examples
  • 🤓Resources
    • Examples
    • Starter Kits
    • Publications
    • Benchmarks
    • Support
  • 📘Contents
    • Host
      • Automatic Public Domain with TLS
    • Configuration
    • Routing
      • Path Parameter Types
      • Reverse Lookups
      • Handle HTTP errors
      • Subdomains
      • Wrap the Router
      • Context Methods
    • HTTP Method Override
    • HTTP Referrer
    • URL Query Parameters
    • Forms
    • Model Validation
    • Cache
    • Cookies
    • Sessions
      • Database
      • Flash Messages
    • Websockets
    • Sitemap
    • Localization
    • Testing
Powered by GitBook
On this page
  • Route naming
  • Route reversing AKA generating URLs from the route name
  • Using route names in code

Was this helpful?

  1. 📘Contents
  2. Routing

Reverse Lookups

PreviousPath Parameter TypesNextHandle HTTP errors

Last updated 1 year ago

Was this helpful?

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

Route naming

Route naming is easy, since we just call the returned *Route with a Name field to define a name:

package main

import "github.com/kataras/iris/v12"

func main() {
    app := iris.New()
    // define a function
    h := func(ctx iris.Context) {
        ctx.HTML("<b>Hi</b1>")
    }

    // handler registration and naming
    home := app.Get("/", h)
    home.Name = "home"
    // or
    app.Get("/about", h).Name = "about"
    app.Get("/page/{id}", h).Name = "page"

    app.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 view engine, we can use a simple action to reverse the routes (and generae actual URLs):

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

Above code would generate the following output:

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

Using route names in code

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

function to get all registered routes

method to retrieve a route by name

method to generate url string based on supplied parameters

method to generate just the path (without host and protocol) portion of the URL based on provided values

Routing
Route
GetRoutes
GetRoute(routeName string)
URL(routeName string, paramValues ...interface{})
Path(routeName string, paramValues ...interface{}