Page routing

The Pages guide uses ProductPage to display a product at /products/42. Two pieces connect that URL to the page. The application tells Atom where to find page classes, and each class declares the URL pattern it handles.

Register Pages

In Atom Starter, app/Application.php already registers @app/Pages. The @app alias points to the application's app directory.

app/Application.php
use Atom\Page\PageRegistry;

protected function pages(PageRegistry $pages): void
{
    $pages->directory("@app/Pages");
}

Put ProductPage.php and ProductPage.atom.html together in app/Pages. When Atom registers the directory, it discovers the page class and reads its #[PageRoute] attribute. That attribute tells Atom to register a GET route for /products/{id}. One pattern handles URLs such as /products/42 and /products/43.

Page Class

The following class uses the Product and ProductService application classes introduced in the Pages guide.

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";
    }
}

When someone opens /products/42:

  1. The router matches /products/{id} and captures 42 as the route parameter named id.
  2. Atom creates ProductPage and injects ProductService into its constructor.
  3. Atom passes the route value to get(string $id) by name. Keep $id aligned with {id} in the route pattern.
  4. get() loads the product and assigns it to $this->product. The method returns void, so Atom renders the adjacent ProductPage.atom.html with the populated page instance.

Mount another page directory

Register a second directory with a prefix when its pages share a URL base.

$pages->directory("@app/AdminPages", "/admin");

A page in that directory with #[PageRoute("/products")] is reachable at /admin/products.

Configure PageRoute and middleware

So far, #[PageRoute("/products/{id}")] only declares the URL pattern. You can also name the route, add a title and description for API Explorer, and attach middleware to that route.

#[PageRoute(
    "/products/{id}",
    name: "products.show",
    middleware: LogProductRequests::class,
    title: "Product details",
    description: "Displays a product by ID."
)]

Place the attribute above ProductPage. The name gives the route a stable identifier for URL generation. The middleware argument accepts one middleware or an array. Atom applies it to the page's GET route and any routes it registers for the page's actions. LogProductRequests represents middleware defined by your application.

To apply middleware to every page in a directory, pass an array as the third argument to directory() in app/Application.php. For example, if your application defines RequireAdmin middleware, you can apply it to the whole admin area:

app/Application.php
use Atom\Page\PageRegistry;

protected function pages(PageRegistry $pages): void
{
    $pages->directory("@app/Pages");
    $pages->directory("@app/AdminPages", "/admin", [
        RequireAdmin::class,
    ]);
}

Directory middleware runs before middleware declared on a page's #[PageRoute]. See Middleware for how the chain works.