Variables
When we have dynamic content to serve, we want our URI to be dynamic as well. This can be easily achieved via path variables. They are defined with a colon, followed by a name (eg. :id
).
Once an incoming request is forwarded to our endpoint, we can access the current value in our param’s action (params[:id]
).
get '/books/:id', to: 'books#show'
Multiple variables can be used in a path.
It’s possible to specify constraints for each variable. The rule MUST be expressed as a regular expression. If a request can satisfy all of them, we’re good, otherwise a 404
is returned.
get '/authors/:id', id: /\d+/, to: 'authors#show'
Optional Tokens
Imagine we want to serve static files from a user repository. It would be impossible to know in advance which files are stored and to prepare routes accordingly.
To solve this problem, Hanami supports wildcard matching.
get '/files/*', to: 'files#show'
We can specify a unique name for each route, in order to generate paths from the router or to test them.
root to: 'home#index'
get '/hello', to: 'greet#index', as: :greeting
When a Hanami application starts, it generates a Ruby module at the runtime under our application namespace: eg. Web.routes
. We can use it to generate a relative or absolute URI for our route.
Web.routes.path(:root) # => "/"
Web.routes.url(:root) # => "http://localhost:2300/"
Web.routes.path(:greeting) # => "/hello"
Web.routes.url(:greeting) # => "http://localhost:2300/hello"
Absolute URL generation is dependent on scheme
, host
and port
settings in apps/web/application.rb
.
Routing Helpers
Generating routes from Web.routes
is helpful, because that module can be accessed from anywhere. However, this syntax is noisy.
Hanami has routing helpers available as routes
in: actions, views and templates.
<%= routes.url(:greeting) %>
Or
<%= routes.greeting_path %>
<%= routes.greeting_url %>
namespace 'docs' do
get '/installation', to: 'docs#installation'
get '/usage', to: 'docs#usage'
end
# This will generate:
#
In case of legacy routes, we can handle HTTP redirects at the routing level.