module Orion::DSL::Match
Overview
Catch-all routes using match
In some instances, you may just want to redirect all verbs to a particular controller and action.
You can use the match
method and pass it's route and
any variation of the Generic Route Arguments.
match "404", controller: ErrorsController, action: error_404
Generic route arguments
There are a variety of ways that you can interact with basic routes. Below are some examples and guidelines on the different ways you can interact with the router.
Using to: String
to target a controller and action
One of the most common ways we will be creating routes in this guide is to use
the to
argument supplied with a controller and action in the form of a string.
In the example below users#create
will map to UsersController.new(cxt : Orion::Server::Context).create
.
You can also pass an exact constant name.
post "users", to: "users#create"
Using controller: Type
and action: Method
A longer form of the to
argument strategy above allows us to pass the controller and action
independently.
post "users", controller: UsersController, action: create
Using block syntax
Sometimes, we may want a more link:https://github.com/kemalcr/kemal[kemal] or link:http://sinatrarb.com/[sinatra] like approach. To accomplish this, we can simply pass a block that will be evaluated as a controller.
post "users" do
"Foo"
end
Using a call
able object
Lastly a second argument can be any
object that responds to #call(cxt : Orion::Server::Context)
.
post "users", ->(context : Orion::Server::Context) {
context.response.puts "foo"
}
Defined in:
orion/dsl/match.crMacro Summary
-
match(path, callable, *, via = :all, helper = nil, constraints = nil, format = nil, accept = nil, content_type = nil, type = nil)
Defines a match route to a callable object.
-
match(path, *, to, via = :all, helper = nil, constraints = nil, format = nil, accept = nil, content_type = nil, type = nil)
Defines a match route to a controller and action (short form).
-
match(path, *, action, controller = CONTROLLER, via = :all, helper = nil, constraints = nil, format = nil, accept = nil, content_type = nil, type = nil)
Defines a match route to a controller and action (long form).
-
match(path, *, via = :all, helper = nil, constraints = nil, format = nil, accept = nil, content_type = nil, type = nil, &block)
Defines a match route with a block.
Macro Detail
Defines a match route to a callable object.
You can route to any object that responds to call
with an Orion::Server::Context
.
module Callable
def call(cxt : Orion::Server::Context)
# ... do something
end
end
match "/path", Callable
Defines a match route to a controller and action (short form).
You can route to a controller and action by passing the to
argument in
the form of "#action"
.
class MyController
def new(@context : Orion::Server::Context)
end
def match
# ... do something
end
end
match "/path", to: "My#match"
Defines a match route to a controller and action (long form).
You can route to a controller and action by passing the controller
and
action
arguments, if action is omitted it will default to match
.
class MyController
def new(@context : Orion::Server::Context)
end
def match
# ... do something
end
end
match "/path", controller: MyController, action: match
Defines a match route with a block.
When given with 0 argument it will yield the block and have access to any method within the BaseController of the application.
When given with 1 argument it will yield the block with Orion::Server::Context
and have access to any method within the BaseController of the application.
When given with 2 arguments it will yield the block with HTTP::Request
and HTTP::Server::Response
and have access to any method within the BaseController of the application.
match "/path" do |context|
# ... do something
end