Routes
Routing in Amber provides developers a manifest to map application URLs to controller actions. By defining routes, you can separate how your application directs requests and how URLs are structured. Each route creates a real-time web socket handler, and define a series of pipeline transformations for scoping middleware to sets of routes.
A route connects a HTTP request to a function inside a controller. When your Amber application receives an incoming request for: GET /users/24
it asks the Amber router to find the corresponding controller action to direct the request towards. If the router finds a match, for example get users/:id, UsersController, :index
, the request will be dispatched to the matching method with the provided parameters, in this case the UsersController.index action with { id: 24 } in the params hash.
Configuring Routes
Routes are configured in the {project_name}/config/routes.cr
file.
Defining Routes
The routes macro accepts a pipeline name and a scope. In addition to the pipeline and scope, the routes macro also takes an additional block parameter where you can define routes. All routes defined within a scope's block will make use of the provided pipeline and the URLs will be scoped accordingly.
For example, let's say you are creating a static website and you want the URL to be displayed as http://www.mycoolsite.com/page
. To do this, you would setup your routes as follows:
If you wanted to created a namespace for nesting your URL routes, you can use the scope parameter to do so.
Mapping the above route
Your controller action will need to return a string or render a view. If no string or view is rendered your routes configuration will raise an error during compilation.
Resources
The router supports other macros besides those for HTTP verbs like get, post, and put. The most important among them is resources
. The resources
macro is a quick way to setup up resourceful routing for all seven standard actions for a controller in a single line.
In order to use resourceful routing for a particular controller, your controller must define and implement all seven standard actions: index
, edit
, new
, show
, create
, destroy
, and update
. If your controller does not implement all seven actions, an error will be raised during compilation.
Let’s add a resource to the config/routes.cr
Then go to the root of your project, and run amber routes
This will output the standard matrix of HTTP verbs, controller, action, pipeline, scope, and URI pattern.
Scoped Routes
Scopes are a way to group routes under a common path prefix and scoped set of pipeline handlers. We might want to do this for admin functionality, APIs, and especially for versioned APIs. Let’s say we have user-generated posts on a site, and that those posts first need to be approved by an admin. The semantics of these resources are quite different, and they might not share the same controller. Scopes enable us to segregate these routes.
The paths to the user facing reviews would look like a standard resource.
But for the admin console paths could be prefixed with /admin.
We accomplish this with a scoped route that sets a path option to /admin like this one. For now, let’s not nest this scope inside of any other scopes (like the scope "/", HelloWeb provides in a new app).
Excluding and Including Actions
Sometimes you want to use resources
as a shortcut for defining routes, and with that you don't want to define routes for actions that don't exist yet. Resources
allow you to pass another argument, only:
or except:
to either include actions or exclude them from being generated.
This will define the following routes:
Namespaces
Namespaces are a way you can add end-points to your routes that aren't tied to resources but are still collected into deeper nested URI paths.
Important: the order you declare your namespace
and resource
routes does matter! You must delcare the namespace routes first, then the resource.
Last updated