Pages

If you’ve built a page with a PHP framework, you’ve probably written a controller action that prepares data and passes it to a view. In an online store, for example, the action for /products/42 might load the product. The view then uses its name, price, and availability to render the product page.

Atom takes that idea a step further by treating the page class as the view model. The class handles the request and defines the properties and methods the template can use while rendering. For example, ProductPage::get() can load the product into $this->product, and the template can read $this->product or call a page method such as $this->availabilityLabel().

By default, Atom’s view locator looks for a template beside the page class. For ProductPage.php, it looks for ProductPage.atom.html. When a visitor opens /products/42, Atom matches the route, runs get(), and renders that template with the page instance available as $this.

Product page example

Put these two files in app/Pages in an Atom Starter project. Open /products/42 to see the sample product.

ProductPage.php
<?php

namespace App\Pages;

use Atom\Page\PageRoute;

#[PageRoute("/products/42")]
final class ProductPage extends AppPage
{
    public string $product = "";
    public bool $inStock = false;

    public function get(): void
    {
        $this->product = "Desk lamp";
        $this->inStock = true;
    }

    public function availabilityLabel(): string
    {
        return $this->inStock ? "In stock" : "Sold out";
    }
}
ProductPage.atom.html
<h1>{{ $this->product }}</h1>
<p>{{ $this->availabilityLabel() }}</p>

The heading reads the $product property. The paragraph calls availabilityLabel() on the same page instance.

Load a product with a service

In a store, the product comes from your application's data. Declare a ProductService dependency in the constructor, and Atom's dependency injector supplies it when it creates the page. Product and ProductService are application classes in this example. The service's getById() method returns a Product with a name and an inStock flag.

When someone opens /products/42, Atom matches /products/{id} and passes the route's id to get(). The handler asks the service for that product and assigns it to the page's $product property.

ProductPage.php
<?php

namespace App\Pages;

use App\Catalog\Product;
use App\Catalog\ProductService;
use Atom\Page\PageRoute;

#[PageRoute("/products/{id}")]
final class ProductPage extends AppPage
{
    public Product $product;

    public function __construct(private readonly ProductService $products)
    {
    }

    public function get(string $id): void
    {
        $this->product = $this->products->getById($id);
    }

    public function availabilityLabel(): string
    {
        return $this->product->inStock ? "In stock" : "Sold out";
    }
}

The adjacent template reads the product's name through $this->product->name and calls $this->availabilityLabel() to render its stock status.

ProductPage.atom.html
<h1>{{ $this->product->name }}</h1>
<p>{{ $this->availabilityLabel() }}</p>

Page State

Our product page so far follows the familiar controller flow. A request comes in, get() loads the product, and the template renders it. Now imagine the shopper wants to choose a quantity before adding the product to their cart. When they press + again, the page needs to know what quantity it showed after the last click.

Atom pages build on the controller pattern with page state and page actions. The #[State] attribute tells Atom which page properties to carry from one action request to the next. The #[PageAction] attribute marks a method that a control in the template can invoke. The quantity lives on the page class, where the template can display it and the actions can update it.

Replace the service-backed example with this complete ProductPage.php. It still loads the product from the service and starts the quantity at 1. The same property also accepts a value from the quantity form.

ProductPage.php
<?php

namespace App\Pages;

use App\Catalog\Product;
use App\Catalog\ProductService;
use Atom\Hydrator\Attributes\FromBody;
use Atom\Page\PageAction;
use Atom\Page\PageRoute;
use Atom\Page\State;

#[PageRoute("/products/{id}")]
final class ProductPage extends AppPage
{
    public Product $product;

    #[State]
    #[FromBody]
    public int $quantity = 1;

    public function __construct(private readonly ProductService $products)
    {
    }

    public function get(string $id): void
    {
        $this->product = $this->products->getById($id);
    }

    public function availabilityLabel(): string
    {
        return $this->product->inStock ? "In stock" : "Sold out";
    }

    #[PageAction]
    public function setQuantity(): void
    {
        $this->quantity = max(1, $this->quantity);
    }

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

    #[PageAction]
    public function decrementQuantity(): void
    {
        $this->quantity = max(1, $this->quantity - 1);
    }
}

Bind a submitted quantity

The input's name="quantity" matches the page property. When the shopper selects Set quantity, Atom first restores the page state, then #[FromBody] binds the submitted value to $quantity as an integer. The setQuantity() action keeps the value at 1 or higher. Atom renders the page again and carries that quantity forward as state.

Add the form and buttons below the stock status. The + and - buttons sit outside the form and send their own actions, so they work from the last accepted quantity.

ProductPage.atom.html - quantity controls
<form method="post" atom:submit="setQuantity">
    <label for="quantity">Set quantity</label>
    <input id="quantity" name="quantity" type="number" min="1" step="1"
           value="{{ $this->quantity }}" required>
    <button type="submit">Set quantity</button>
</form>

<div role="group" aria-label="Quantity controls">
    <button type="button" atom:action="decrementQuantity" aria-label="Decrease quantity">−</button>
    <output aria-label="Quantity" aria-live="polite">{{ $this->quantity }}</output>
    <button type="button" atom:action="incrementQuantity" aria-label="Increase quantity">+</button>
</div>

Open /products/42 in an Atom Starter app with the sample product classes in place. Type 3 and select Set quantity. The output shows 3. Press + to get 4, then - to return to 3. Typing in the field only edits the browser input until you submit the form.

On each button click, Atom restores the last quantity, reloads the product in get(), and runs the requested action. Keep the initial value on the property so get() leaves the restored quantity in place. A fresh visit starts at 1.

Summary

Feature Role in the product page
#[PageRoute] and get() Match /products/{id} and load the matching product through ProductService.
Adjacent .atom.html template Render the product name and stock label using properties and methods on $this.
#[State] Carry the current quantity into the next action request.
#[FromBody] Bind the submitted quantity field to the page property.
#[PageAction] Run the method named by atom:submit on the form or atom:action on a button.