Route API
The Routing guide registers two routes directly in the application. This example builds on that approach by grouping the article routes under a shared prefix, binding request values to handler parameters, and adding metadata to the article lookup.
Declare and group routes
Add the imports and put the group inside bootstrap() in app/Application.php. Replace the earlier direct preview route with this grouped version and keep the health check.
use Atom\Http\Request;
use Atom\Hydrator\Attributes\FromQuery;
use Atom\Hydrator\Attributes\FromRoute;
use Atom\Router\Route;
// Inside bootstrap().
Route::group("/api/articles", static function (): void {
Route::get("/{slug}", static function (
#[FromRoute] string $slug,
#[FromQuery] string $view = "summary",
): array {
return ["slug" => $slug, "view" => $view];
})
->name("articles.show")
->title("Read an article")
->description("Look up an article by its slug.");
Route::post("/preview", static function (Request $request): array {
return ["title" => trim($request->post()->string("title")), "published" => false];
});
});
Use helpers such as Route::get(), post(), put(), patch(), delete(), head(), and options() when a route handles one HTTP method. For a handler shared by several methods, Route::addRoute() accepts an array such as ["GET", "HEAD"]. A route handler can be a closure or a class-and-method pair.
Route::group() prefixes the paths declared inside its callback. Here the group registers GET /api/articles/{slug} and POST /api/articles/preview.
Bind parameters
For route and query attributes, match the PHP parameter name to the request key or pass the source name explicitly, such as #[FromQuery("view")] on $display. Inject Request when you want to read a form field directly. For typed form binding with #[FromBody], see Hydration.
| Attribute | Reads from | In this example |
|---|---|---|
#[FromRoute] |
A placeholder in the URL path | /api/articles/building-atom supplies building-atom as $slug. |
#[FromQuery] |
A query string key | ?view=full supplies full as $view. When the key is omitted, the parameter defaults to summary. |
$request->post()->string("title") |
A field in the parsed form body | Reads the submitted title as a string. |
Add route metadata
The registration returns a route entry that you can configure with chained calls. name() gives the route an identifier for URL generation. title() and description() describe it for tools such as API Explorer. The article lookup above uses all three.
Generate a URL by name
Once the route has been registered, use Route::url() to build its site-relative URL. Pass the path parameters and any query parameters separately. The result includes the group prefix.
$url = Route::url(
"articles.show",
["slug" => "building-atom"],
query: ["view" => "full"],
);
// /api/articles/building-atom?view=full
Attach controllers
When related handlers move into a controller, register it with Route::attach(ArticleController::class). Use Route::attachTo("/v1", ArticleController::class) to add a prefix before the controller's own path. A controller can also be attached inside a group to inherit that group's prefix.
Controllers shows how the controller and method attributes map to the article endpoints.