Build your first page

Build a counter at /counter in an Atom Starter app. From the project root, put both files in the existing app/Pages directory, next to AppPage.php.

app/Pages/
    AppPage.php
    CounterPage.php
    CounterPage.atom.html

The PHP class holds the count and defines what a click does. The template displays that count and supplies the button.

Define the PHP page

Create app/Pages/CounterPage.php. The Starter's pages() hook in app/Application.php already registers @app/Pages. During startup, Atom discovers CounterPage there and uses #[PageRoute("/counter")] to register its URL.

The page extends AppPage, so it inherits the Starter's shared layout. The #[State] attribute tells Atom to carry the count between actions.

<?php

declare(strict_types=1);

namespace App\Pages;

use Atom\Page\PageAction;
use Atom\Page\PageRoute;
use Atom\Page\State;

#[PageRoute("/counter")]
final class CounterPage extends AppPage
{
    public string $title = "Counter";

    #[State]
    public int $count = 0;

    #[PageAction]
    public function increment(): void
    {
        ++$this->count;
    }
}

Add the template

Create app/Pages/CounterPage.atom.html beside the PHP file. Atom looks for a template with the same base name as the class. Inside it, $this is the current CounterPage instance, so the output can read $this->count. The button's atom:action="increment" names the method marked with #[PageAction].

<h1>Counter</h1>
<output aria-label="Current count" aria-live="polite">{{ $this->count }}</output>
<button type="button" atom:action="increment">Increment</button>

Try the action

Start the app with php atom serve and open /counter on the printed address. The first GET renders 0. When you click Increment:

  1. The Atom client, loaded by the shared layout, sends a POST to /counter with the action name and the current page state.
  2. Atom creates a new page instance, restores $count from the submitted state, and calls increment() in PHP.
  3. Atom renders the template with the new count. The client applies the returned HTML and carries the new state into the next click.

Press Increment twice to see 0 → 1 → 2. A fresh GET starts at 0 again.

Make the new page reachable from anywhere in the Starter app. In app/Components/SiteLayout.atom.html, find the navigation element labelled Primary navigation. Add this link immediately after Home.

<a href="/counter" atom:navigate>Counter</a>

Open / and select Counter in the menu. The address changes to /counter and the page appears inside the layout's <main atom:update-root>. The shared header stays in place. Use the browser's Back button to return to the homepage.

Click Counter again, then Increment. The count changes from 0 to 1, just as it does when you open /counter directly.