Skip to content

module Athena::Routing::Generator::Interface #

Allows generating a URL for a given ART::Route.

routes = ART::RouteCollection.new
routes.add "blog_show", ART::Route.new "/blog/{slug}"

generator = ART::Generator::URLGenerator.new context
generator.generate "blog_show", slug: "bar-baz" # => "/blog/bar-baz"

Query Parameters#

If a parameter passed in via params does not map to a known route parameter (path, hostname, etc) it'll be added as a query parameter. For example, using the route defined above:

generator.generate "blog_show", slug: "bar-baz", source: "Crystal" # => "/blog/bar-baz?source=Crystal"

The special _query parameter may be used to explicitly add query parameters. This can be useful when a query parameter may conflict with a route parameter of the same name. For example, given a route like https://{siteCode}.{domain}/admin/stats:

generator
  .generate(
    "admin_stats",
    {
      "siteCode" => "fr",
      "domain"   => "example.com",
      "_query"   => {
        "siteCode" => "us",
      },
    },
  ) # => "https://fr.example.com/admin/stats?siteCode=us"

Parameter Default Values#

By default parameters with a default value the same as the provided parameter will be excluded from the generated URL. For example:

routes = ART::RouteCollection.new
routes.add "articles", ART::Route.new "/articles/{page}", {"page" => "1"}

ART.compile routes

generator = ART::Generator::URLGenerator.new ART::RequestContext.new
generator.generate "articles"          # => "/articles"
generator.generate "articles", page: 1 # => "/articles"
generator.generate "articles", page: 2 # => "/articles/2"

If you want to always include a parameter, add a ! before the ART::Route#path, for example:

routes.add "users", ART::Route.new "/users/{!page}", {"page" => "1"}

generator.generate "users"          # => "/users/1"
generator.generate "users", page: 1 # => "/users/1"
generator.generate "users", page: 2 # => "/users/2"

URL Types#

Athena::Routing supports various ways to generate the URL, via the reference_type parameter. See ART::Generator::ReferenceType for description/examples of the possible types.

Included modules

Athena::Routing::RequestContextAwareInterface

Direct including types

Athena::Routing::Generator::URLGenerator Athena::Routing::RouterInterface

Methods#

abstract #generate(route : String, params : Hash = Hash(String, String | ::Nil).new, reference_type : ART::Generator::ReferenceType = :absolute_path) : String#

Generates a URL for the provided route, optionally with the provided params and reference_type.

View source

abstract #generate(route : String, reference_type : ART::Generator::ReferenceType = :absolute_path, **params) : String#

Generates a URL for the provided route, optionally with the provided params and reference_type.

View source