{"title":"Routes","description":"Define Amber V2 paths, resources, namespaces, and constraints","section":"guides/routing","version":"v2","path":"guides/routing/routes","canonical_url":"https://amberframework.org/docs/v2/guides/routing/routes","markdown_url":"https://amberframework.org/docs/v2/guides/routing/routes.md","inherited":false,"content_markdown":"# Routes\n\nDefine routes inside `Amber::Server.configure` and attach each group to a named\npipeline.\n\n**File: `config/routes.cr` — add these declarations inside the generated\n`Amber::Server.configure` block. Keep the existing `:static` routes.**\n\n```crystal\nAmber::Server.configure do\n  routes :web do\n    get \"/posts\", PostsController, :index\n    get \"/posts/:id\", PostsController, :show\n    post \"/posts\", PostsController, :create\n    patch \"/posts/:id\", PostsController, :update\n    delete \"/posts/:id\", PostsController, :destroy\n  end\nend\n```\n\nDynamic segments such as `:id` are available through `params` in the action.\nAmber also supports `put`, `options`, `head`, `trace`, and `connect` route\nmacros.\n\n## Resource routes\n\n`resources` creates conventional routes for `index`, `new`, `create`, `show`,\n`edit`, `update`, and `destroy`:\n\n**File: `config/routes.cr` — use these entries inside an existing\n`Amber::Server.configure` block, as an alternative to listing every route.**\n\n```crystal\nroutes :web do\n  resources \"/posts\", PostsController\n  resources \"/profiles\", ProfilesController, only: [:show, :edit, :update]\n  resources \"/events\", EventsController, except: [:destroy]\nend\n```\n\nOnly declare actions implemented by the controller; missing resource actions\nfail during compilation.\n\n## Scopes and namespaces\n\nA scope on `routes` prefixes the complete group. Nested `namespace` blocks add\nanother path segment.\n\n**File: `config/routes.cr` — add this route group inside\n`Amber::Server.configure`.**\n\n```crystal\nroutes :api, \"/api\" do\n  namespace \"/v1\" do\n    resources \"/posts\", Api::PostsController, only: [:index, :show]\n  end\nend\n```\n\n## Segment constraints\n\nConstrain a dynamic segment with a regular expression when a route must reject\nnon-matching values.\n\n**File: `config/routes.cr` — add the constrained route inside the existing\n`:web` route group.**\n\n```crystal\nroutes :web do\n  get \"/orders/:id\", OrdersController, :show, {\"id\" => /\\d+/}\nend\n```\n\nRun `amber routes` from the project root to print the declared route table. Pair\nthat inspection with request specs and the compiler to verify dispatch behavior."}