# Routes

Define routes inside `Amber::Server.configure` and attach each group to a named
pipeline.

**File: `config/routes.cr` — add these declarations inside the generated
`Amber::Server.configure` block. Keep the existing `:static` routes.**

```crystal
Amber::Server.configure do
  routes :web do
    get "/posts", PostsController, :index
    get "/posts/:id", PostsController, :show
    post "/posts", PostsController, :create
    patch "/posts/:id", PostsController, :update
    delete "/posts/:id", PostsController, :destroy
  end
end
```

Dynamic segments such as `:id` are available through `params` in the action.
Amber also supports `put`, `options`, `head`, `trace`, and `connect` route
macros.

## Resource routes

`resources` creates conventional routes for `index`, `new`, `create`, `show`,
`edit`, `update`, and `destroy`:

**File: `config/routes.cr` — use these entries inside an existing
`Amber::Server.configure` block, as an alternative to listing every route.**

```crystal
routes :web do
  resources "/posts", PostsController
  resources "/profiles", ProfilesController, only: [:show, :edit, :update]
  resources "/events", EventsController, except: [:destroy]
end
```

Only declare actions implemented by the controller; missing resource actions
fail during compilation.

## Scopes and namespaces

A scope on `routes` prefixes the complete group. Nested `namespace` blocks add
another path segment.

**File: `config/routes.cr` — add this route group inside
`Amber::Server.configure`.**

```crystal
routes :api, "/api" do
  namespace "/v1" do
    resources "/posts", Api::PostsController, only: [:index, :show]
  end
end
```

## Segment constraints

Constrain a dynamic segment with a regular expression when a route must reject
non-matching values.

**File: `config/routes.cr` — add the constrained route inside the existing
`:web` route group.**

```crystal
routes :web do
  get "/orders/:id", OrdersController, :show, {"id" => /\d+/}
end
```

Run `amber routes` from the project root to print the declared route table. Pair
that inspection with request specs and the compiler to verify dispatch behavior.