Skip to content

class Athena::Routing::URLGenerator
inherits Reference #

Default implementation of ART::URLGeneratorInterface.

Included modules

Athena::Routing::URLGeneratorInterface

Constructors#

.new(routes : ART::RouteCollection, request : ATH::Request, base_uri : URI?) #

View source

Methods#

#generate(route : String, params : Hash(String, _)? = nil, reference_type : ART::URLGeneratorInterface::ReferenceType = :absolute_path) : String #

Generates a URL to the provided route with the provided params.

By default the path is an ART::URLGeneratorInterface::ReferenceType::Absolute_Path, but can be changed via the reference_type argument.

Any params not related to an argument for the provided route will be added as query params.

require "athena"

class ExampleController < ATH::Controller
  @[ATHA::Get("/add/:value1/:value2", name: "add")]
  def add(value1 : Int32, value2 : Int32, negative : Bool = false) : Int32
    0
  end

  @[ATHA::Get("/")]
  def get_link : String
    ""
  end
end

generator.generate "add", value1: 10, value2: 5 # => /add/10/5

params are validated to ensure they are all provided, and meet any route constraints defined on the action.

Todo

Make URL generation more robust.

ameba:disable Metrics/CyclomaticComplexity

View source