Most content management systems keep their content model in the database: a screen in the admin adds a field, and the field is a row somewhere that nobody reviews. Mainstay keeps it in PHP. A content type is a class extending Entry, each of its fields a typed property with an attribute saying what kind of field it is, and the class is registered in a service provider. That is the whole declaration.
#[Route('/blog/{slug}')]
class Article extends Entry
{
#[Text(required: true, localized: true)]
public string $slug;
#[Textarea(required: true, localized: true)]
public string $summary;
#[Date(required: true)]
public CarbonImmutable $publishedOn;
#[Text(max: 40)]
public ?string $glyph;
#[Terms(of: Tag::class)]
public array $tags;
}
Mainstay::types([Page::class, Article::class, Tag::class]) in a service provider's boot() registers it. Types are registered, not discovered: there is no directory Mainstay scans and no config array of them, so a type exists because a line of code says so.
One declaration, read four ways
Reflection reads the class once into a field list, and everything downstream is derived from that list rather than written beside it:
The database. Each field type names its column, so
#[Text(max: 40)] public ?string $glyphis avarchar(40)that allows null, because the property does.Validation. The same field writes the rules,
stringandmax:40. The limit an editor is held to and the limit the column enforces are one number, so they cannot disagree.The admin. Each field type names the Blade component that draws it, so the edit form is drawn from the class and has no definition of its own.
The API. Each field type describes itself as a JSON Schema fragment. The content API serves the whole schema at
/schema, andmainstay:typescriptwrites it out as TypeScript declarations for a front end.
The property's PHP type is part of the declaration too. ?string is an optional string, string a value every entry has. A field type checks the property it sits on, so #[Text] public int $count is refused while the type is read, with a message naming the attribute, rather than failing later with a type error two frames away from anything that mentions it.
What was weighed against it
In PHP the class is the type. $article->summary is a string to an editor's autocomplete and to static analysis with no generate step, and code generation is only needed where content crosses into another language, which is what the TypeScript declarations are for.
The other way is the database-defined model, where a screen builds content types. It lets an editor add a field without a developer, and the model can change on a live site with no deploy. The cost is that the model lives in rows: a field can arrive on production without a review, the template that should print it is written later or never, and two environments can drift apart one click at a time. Mainstay has no content_types table and no screen to build a type, so a field is reviewed and ships with the template that prints it. The admin draws forms from declarations; it never authors them.
The trade
A field is a property on a class, so adding one is a code change, and an editor cannot add one without a developer. That is the trade Mainstay chooses. A field shows up in a code review, it ships with the template that prints it, and the database follows it, through mainstay:sync in development and a migration you write for production.
Attributes are less expressive than an arbitrary configuration object, so a nested structure points at another class instead of nesting inline. Every block ends up as its own typed class with its own fields, so a block is as checkable as an entry.
A field type of your own
A field type is a class extending Field, and the attribute on the property is an instance of it. There is no registration call: the instance is already on the property when reflection reads it, so nothing ever looks a field type up by name. A field type of your own answers the same questions the built-in ones do: its column, its rules, its JSON Schema and the component that draws it in the admin.
Every entry type holds a title and a content without declaring them: the title is what the admin names an entry by, the content its text and blocks.