This project is under very early, active development and may contain bugs or security issues. It is likely not ready for production websites.

You are responsible for reviewing, testing, and securing any deployment. Ava CMS is provided as free, open-source software without warranty (GNU General Public License), see LICENSE.

Routing

Routing is how your CMS decides which content to show when someone visits a URL on your site — like matching /about to your about page.

In Ava CMS, you don't write route files. URLs are generated automatically based on your content structure and configuration.

How It Works

  1. 💾 Save a file in your content directory
  2. 👀 Ava indexes it (automatically in auto index mode or via ./ava rebuild)
  3. ✨ The URL works — routing is handled for you

Routes are compiled into a binary cache for instant lookups.

URL Styles

Choose how URLs are generated per content type in app/config/content_types.php.

Hierarchical URLs

Best for most simple content types that aren't organised by date. URLs mirror your file structure.

'page' => [
    'url' => [
        'type' => 'hierarchical',
        'base' => '/',
    ],
]

How it maps files to URLs:

File Path URL
content/pages/about.md /about
content/pages/about/team.md /about/team
content/pages/services/web.md /services/web
content/pages/index.md / (homepage)
content/pages/docs/index.md /docs

Key points:

  • index.md files represent the parent folder URL
  • base adds a URL prefix (e.g., 'base' => '/docs' makes about.md → /docs/about)
  • The file path determines the URL — slug: in frontmatter is ignored
  • Use path-based keys for lookups: $ava->get('page', 'about/team')

Pattern URLs

Best for blogs and news. URLs follow a specified pattern regardless of file organization.

'post' => [
    'url' => [
        'type'    => 'pattern',
        'pattern' => '/blog/{slug}',
        'archive' => '/blog',  // Optional: listing page URL
    ],
]

Available placeholders:

Placeholder Description Example
{slug} Item's slug from frontmatter or filename my-post
{id} Item's unique ID (ULID or UUID7) 01HXYZ4567ABCD
{yyyy} 4-digit year from date field 2024
{mm} 2-digit month from date field 03
{dd} 2-digit day from date field 15

Examples:

'/blog/{slug}'                        // → /blog/my-post
'/blog/{yyyy}/{slug}'                 // → /blog/2024/my-post
'/news/{yyyy}/{mm}/{dd}/{slug}'       // → /news/2024/03/15/my-post
'/p/{id}'                             // → /p/01HXYZ4567ABCD (rename-proof)

Key points:

  • Use the slug field for lookups: $ava->get('post', 'hello-world')
  • File organization doesn't affect URLs
  • archive creates a listing page

Content Status and Routing

Content files must have a status in their frontmatter to determine routing behavior:

---
title: Sample Post
status: published  # draft, published, unlisted
---

Depending on the status, routing behaves as follows:

Status Routing Behavior
draft Not routed publicly. Only accessible via preview mode.
published Fully accessible. Appears in listings and sitemaps.
unlisted Accessible via direct URL but excluded from listings.

Preview Mode

View draft content before publishing with a signed preview link. Generate one with the CLI:

./ava preview /blog/my-draft
./ava preview content/posts/my-draft.md --hours=24

A signed link works for that URL only and expires (after 72 hours by default; --hours=N changes it), so it can be shared without sharing your preview token. It looks like /blog/my-draft?preview=<expires>-<signature>.

Configure in ava.php:

'security' => [
    'preview_token' => 'your-secure-random-token',
]

Generate a secure token:

php -r "echo bin2hex(random_bytes(32));"

The token signs preview links, so it must be at least 24 characters. A shorter value leaves preview mode disabled, and ./ava lint and ./ava preview report it. Without a configured token, preview mode is disabled.

You can also add ?preview=1&token=YOUR_SECRET_TOKEN to a draft's URL; this is still supported. Both parameters are required. This form works for every draft and never expires, and it puts the token itself in the address. Prefer ./ava preview links, which cover one URL, expire, and keep the token out of the address.

Redirects

Redirect old URLs when you move or rename content:

---
title: New Page Title
redirect_from:
  - /old-page
  - /legacy/old-url
---

Ava issues 301 (permanent) redirects from old URLs to the current URL. For external redirects or arbitrary mappings, use the Redirects plugin.

Trailing Slash

Enforce consistent URL format in ava.php:

'routing' => [
    'trailing_slash' => false,  // /about (recommended)
]

Ava issues 301 redirects to enforce your choice:

  • false: /about/ → /about
  • true: /about → /about/

The root / is always valid. Repeated trailing slashes (/about//) redirect to the same canonical form. The query string is kept.

Taxonomy Routes

Taxonomies with public: true automatically get routes:

'category' => [
    'label'   => 'Categories',
    'public'  => true,         // Enable public routes
    'rewrite' => [
        'base' => '/category', // URL prefix
    ],
],

Routes created:

  • /category — Taxonomy index (all terms)
  • /category/tutorials — Term archive (items with this term)

Template variables:

  • $tax['name'] — Taxonomy name
  • $tax['term'] — Term data (term archives)
  • $tax['terms'] — All terms (index)
  • $query — Pre-filtered Query object

See: Taxonomies for full documentation on configuration, term storage, and template examples.

Adding Custom Routes

Register custom routes in your theme.php for APIs or special pages.

Exact Routes

return function (\Ava\Application $app): void {
    $router = $app->router();
    
    $router->addRoute('/api/search', function ($request) use ($app) {
        $results = $app->query()
            ->published()
            ->search($request->query('q', ''))
            ->perPage(10)
            ->get();
        
        return \Ava\Http\Response::json([
            'results' => array_map(fn($item) => [
                'title' => $item->title(),
                'url' => $app->router()->urlFor($item->type(), $item->slug()),
            ], $results),
        ]);
    });
};

Routes with Parameters

$router->addRoute('/api/posts/{id}', function ($request, $params) use ($app) {
    $item = $app->repository()->getById($params['id']);
    
    if (!$item) {
        return \Ava\Http\Response::json(['error' => 'Not found'], 404);
    }
    
    return \Ava\Http\Response::json(['title' => $item->title()]);
});

### Prefix Routes

```php
$router->addPrefixRoute('/api/', function ($request) use ($app) {
    // Handles all /api/* requests
    return \Ava\Http\Response::json(['path' => $request->path()]);
});

Prefix routes are checked before exact routes, trailing-slash enforcement and content routes.

Using Hooks

Intercept routing with the router.before_match filter (checked first):

use Ava\Plugins\Hooks;
use Ava\Http\Response;

Hooks::addFilter('router.before_match', function ($match, $request) use ($app) {
    if ($request->path() === '/custom-page') {
        return Response::html($app->render('custom-template'));
    }
    return $match;
});

See Understanding Hooks for details.

Route Caching

Routes are compiled to binary cache files in the live index generation (storage/cache/index/<generation>/) for instant lookups:

  • routes.bin — All route mappings
  • slug_lookup.bin — Content key lookups

Rebuilding:

  • content_index.mode = 'auto' — Automatic when content changes
  • content_index.mode = 'never' — Run ./ava rebuild manually

See Performance for details.

Generating URLs in Templates

Use $ava helper methods:

<?= $ava->url('post', 'hello-world') ?>       // Content: /blog/hello-world
<?= $ava->url('page', 'about/team') ?>        // Hierarchical: /about/team
<?= $ava->termUrl('category', 'tutorials') ?> // Term: /category/tutorials
<?= $ava->fullUrl('/about') ?>                // Full: https://example.com/about
<?= $ava->baseUrl() ?>                        // Base: https://example.com

Route Matching Order

When a request comes in, Ava CMS checks routes in this specific order:

  1. Canonical encoding — GET and HEAD requests whose path isn't in canonical percent-encoding get a 301 to the canonical spelling
  2. Hook interception — router.before_match filter can intercept and return a response early
  3. Prefix routes — Custom routes registered via $router->addPrefixRoute()
  4. Trailing slash redirect — Enforces your canonical URL style (301 redirect)
  5. Redirects — 301 redirects from redirect_from frontmatter
  6. System routes — Custom routes registered via $router->addRoute(), exact then parameterized
  7. Method check — Content pages answer only the methods in routing.content_methods (GET and HEAD by default); anything else gets 405 with an Allow header. Hooks, prefix routes and plugin routes above this step still receive every method.
  8. Exact routes — Content URLs from the routes cache
  9. Preview mode — Allows draft access with a valid preview link or token
  10. Taxonomy routes — Archives like /category/tutorials
  11. 404 — No match found

Understanding this order helps when debugging why a route isn't matching as expected.

Prefix routes run before trailing-slash enforcement, so asset or API URLs such as /theme/... are not subject to the routing.trailing_slash setting.

On content pages that aren't archives or taxonomy term pages, a request with ?paged=... gets a 302 redirect to the same URL without paged; other query parameters are kept. Plugin routes, feeds, sitemaps and the theme's /search route are never redirected. A template that paginates its own query declares it with $ava->paginated(); see Theming - Paginating your own query on a page.

Debugging Routes

Inspect route cache:

$routes = $app->repository()->routes();
var_dump($routes['exact']['/my-path'] ?? 'not found');

Common issues:

Issue Solution
404 for new content Run ./ava rebuild or set content_index.mode = 'auto'
Wrong hierarchical URL Use path-based key: $ava->get('page', 'about/team')
Redirect loop Check for circular redirect_from
Preview not working Verify security.preview_token in config (at least 24 characters)