With your favorite editor open and add the following line.
Then start the server with bundle exec hanami server
and visit . You should see Hello from Hanami!
in your browser.
Let’s explain what we just did. We created a route; an application can have many routes. Each route starts with an HTTP verb declaration, get
in our case. Then we specify a relative URI (/hello
for us) and the object that is responsible to respond to incoming requests.
We can use most common HTTP verbs: GET
, POST
, PUT
, PATCH
, DELETE
, TRACE
and OPTIONS
.
endpoint = ->(env) { [200, {}, ['Hello from Hanami!']] }
get '/hello', to: endpoint
post '/hello', to: endpoint
patch '/hello', to: endpoint
delete '/hello', to: endpoint
trace '/hello', to: endpoint
A valid endpoint can be an object, a class, an action, or an application that responds to #call
.
When we use a string, it tries to instantiate a class from it:
get '/rack-app', to: 'rack_app' # it will map to RackApp.new
Full Rack integration is great, but the most common endpoint that we’ll use in our web applications is an action. Actions are objects responsible for responding to incoming HTTP requests. They have a nested naming like Web::Controllers::Home::Index
. This is a really long name to write, that’s why Hanami has a naming convention for it: "home#index"
.
The first token is the name of the controller "home"
is translated to Home
. The same transformation will be applied to the name after the #
: "index"
to Index
.
If we want to mount an application, we should use within the Hanami environment configuration file. The global configuration file is located at config/environment.rb
. Place mount
within the Hanami.configure
block.
Hanami.configure do
mount Web::Application, at: '/'
mount OtherApplication.new, at: '/other'
...
Mounting To A Path
All the HTTP requests starting with /sinatra
will be routed to SinatraApp
.
Mounting On A Subdomain
mount Blog.new, host: 'blog'
All the HTTP requests to http://blog.example.com
will be routed to Blog
.
In development, you will NOT be able to access http://blog.localhost:2300
, so you should specify a host when running the server: . Then your application can be visited at http://blog.lvh.me:2300