AkurAI Build
Menu

Public repository / dbd1e2529b1a8875ac975415d54ef3e98e046457

AkurAI-Framework / site/content/docs/routing.md

Latest change 0ebc2057fd9a65182855334c219522ae944ad484 - docs: /docs guides for routing, forms, storage, middleware, SEO, styling + dev server by Ólafur Búi Ólafsson

# Routing

The server resolves a request path to a template through a small, declarative
route table, with dynamic segments captured as params. Routing is pure-Rust and
needs no build step.

## Declaring routes

Routes live in `backend/routes.json` — a list of `{ path, template }` entries:

```json
{
  "routes": [
    { "path": "/",            "template": "index" },
    { "path": "/posts/:id",   "template": "post"  },
    { "path": "/files/*path", "template": "file"  }
  ]
}
```

Each `template` names a `frontend/<template>.html` file. The file is optional:
without `routes.json`, the server falls back to its legacy behaviour (a template
named after the last path segment), so existing projects are unaffected.

## Pattern segments

- **static** — `posts` — must match the path segment exactly.
- **`:param`** — captures exactly one segment by name.
- **`*wildcard`** — captures the rest of the path (only meaningful as the final
  segment).

When several patterns match one path, the **most specific wins**: static beats
`:param` beats `*wildcard`. So `/posts/new` matches a literal `/posts/new` route
even when `/posts/:id` also exists.

## Using params in a template

Captured params arrive in the render context under `params`, so a template can
read them directly. For the route `/hello/:name`:

```html
<h1>Hello, {{ params.name }}</h1>
```

Values rendered with `{{ }}` are HTML-escaped automatically. There is a live
demo at `/hello/world`.

## Unmatched paths

A request that matches no route renders the `index` template — the SPA fallback,
so client-side routing can take over from there.

## What's next

Per-route data **loaders** (a server function whose result feeds the template)
and **layouts** (a shared shell template wrapping a page) build on this table;
they're on the roadmap.