Mainstay GitHub
All docs

Routes and templates

How a path finds its entry, the view the entry renders with, and the front page.

Public pages are served by one catch-all route. It takes the locale from the request's host and path prefix, finds the entry the rest of the path leads to, and renders that entry's view. A path that leads nowhere is an ordinary 404.

Declaring a path

#[Route] on a type builds each entry's path from its fields:

#[Route('/docs/{slug}')]

A {field} takes the field's value. Every segment is lowercase letters and numbers between hyphens, what Str::slug() writes, and a routed field's values and a routed select's options are held to it. Each save writes the entry's path into a lookup table, uris, so finding a page is one indexed read whatever the type, and two entries cannot claim one path. $entry->url() is the locale's base and that path.

Your routes come first

The catch-all is a fallback route, so any route your application declares wins whatever order it was registered in. A path with a trailing slash is sent to the one without, so every page has one address. Its middleware is site.middleware in the config, web by default; set site.enabled to false where the front end is built elsewhere.

The view

An entry renders with its own view if it names one, else its type's #[Template], else the view named after its type's handle: article.blade.php for Article. It gets the entry as $entry.

app/Content/Doc.php
#[Route('/docs/{slug}')]
#[Template('docs.page', 'docs.wide')]
class Doc extends Entry
{
    // ...
}

The first view is the one entries render with; any after it is one an entry may pick as its own instead, and no other, since a view is written against the type's fields. A view named and missing is an error, never a quiet step down to another.

The front page

Any entry of a routed type can be the front page, served at /. Choose it with Use as front page in the menu on the entry's form, or from code:

Mainstay::setFrontPage(Page::class, $home->id);

Its path becomes / in every language it has, and the page it replaces takes its own path again. The front page cannot be moved to the trash until another is chosen. Mainstay::frontPage() gives its type and id.

Changing a slug

A save rebuilds the path from the pattern, and the old path stops answering. Redirects from old paths are not built.