Controllers

When several route handlers belong to the same feature, put them in a controller. The controller below keeps the article lookup and preview together. Atom reads the class and method attributes when you attach it to the router.

Route requests to a controller

Create app/Controllers/ArticleController.php. Each handler returns a small array so you can see which request value it received.

app/Controllers/ArticleController.php
<?php

declare(strict_types=1);

namespace App\Controllers;

use Atom\Http\Request;
use Atom\Hydrator\Attributes\FromRoute;
use Atom\Router\Attributes\Controller;
use Atom\Router\Attributes\Get;
use Atom\Router\Attributes\Head;
use Atom\Router\Attributes\Post;

#[Controller("/api/articles")]
final class ArticleController
{
    #[Get("{slug}")]
    #[Head("{slug}")]
    public function show(#[FromRoute] string $slug): array
    {
        return ["slug" => $slug];
    }

    #[Post("preview")]
    public function preview(Request $request): array
    {
        return ["title" => $request->post()->string("title")];
    }
}

Map attributes to routes

#[Controller("/api/articles")] sets the prefix for this class. Each method attribute declares an HTTP method and a path relative to that prefix. A handler can have more than one route attribute.

Method attribute Registered route Handler
#[Get("{slug}")] GET /api/articles/{slug} show()
#[Head("{slug}")] HEAD /api/articles/{slug} show()
#[Post("preview")] POST /api/articles/preview preview()

For /api/articles/building-atom, #[FromRoute] binds building-atom to $slug. Both GET and HEAD call show(). The preview handler reads the submitted title from the injected Request.

Attach the controller

Import App\Controllers\ArticleController, Atom\Di\Injector, and Atom\Router\Route in app/Application.php. In the existing bootstrap() hook, replace the article route declarations from the Route API example with Route::attach(). Keep the health check.

app/Application.php (excerpt)
protected function bootstrap(Injector $injector): void
{
    Route::get("/api/health", static fn(): array => ["status" => "ok"]);
    Route::attach(ArticleController::class);
}

If you used the introductory ApiController, remove its attachment and keep the health check shown here so the article preview is registered once.

Add metadata and middleware

Use named arguments on controller and method attributes to configure the routes that Route::attach() registers. The class attribute can apply middleware to every route in the controller. Method attributes can add middleware and describe individual routes.

Here ArticleRequestLogging and ArticleReadMetrics are application middleware classes. Extend the attributes on ArticleController and its existing show() method as follows:

app/Controllers/ArticleController.php (excerpt)
use App\Middleware\ArticleReadMetrics;
use App\Middleware\ArticleRequestLogging;

// On the controller class.
#[Controller("/api/articles", middleware: ArticleRequestLogging::class)]

// On show().
#[Get(
    "{slug}",
    name: "articles.show",
    title: "Read an article",
    description: "Look up an article by its slug.",
    middleware: ArticleReadMetrics::class,
)]
#[Head("{slug}")]

The name identifies the GET route for URL generation. The title and description describe the route in tools such as API Explorer when it is marked as an API route.

Controller middleware runs before method middleware. Here ArticleRequestLogging applies to GET, HEAD, and POST. ArticleReadMetrics applies only to GET because it is declared on #[Get]. Each method attribute registers its own route, so set those options separately on #[Head] if needed. The middleware argument can take a single middleware or an array.

Mount the same API under a version prefix

Use Route::attachTo("/v1", ArticleController::class) in place of attach() when the article endpoints share a version prefix. Atom prepends /v1 to the class prefix, so the lookup becomes GET /v1/api/articles/{slug}. The health check is registered separately and stays at /api/health.