Routing
A route connects an HTTP method and a URL path to a PHP handler. If you have used a PHP framework with a separate routes file, the place to start in Atom is the application class. In a small Atom application, you can register routes in the bootstrap() hook in app/Application.php. The web entry point, public/index.php, starts the application.
Here are the files involved:
my-atom-app/
├── app/
│ └── Application.php
├── public/
│ └── index.php
└── composer.json
composer.jsondeclares the project's dependencies and how Composer loads the application's PHP classes.public/index.phpis the front controller. It starts Atom and sends the response back to the client.app/Application.phpconfigures the application. Itsbootstrap()hook is where you'll register the GET and POST routes below.
The tree shows the files involved in this walkthrough. If you started with Atom Starter, use its existing files and add the route registrations to Application.php.
Declare routes in the application
Here is a minimal application class with two route declarations. If you already have an application class, add the declarations inside its existing bootstrap() hook.
<?php
namespace App;
use Atom\Di\Injector;
use Atom\Router\Route;
final class Application extends \Atom\Application
{
protected function rootPath(): string
{
return dirname(__DIR__);
}
protected function bootstrap(Injector $injector): void
{
Route::get("/api/health", static fn(): array => ["status" => "ok"]);
Route::post("/api/articles/preview", static fn(): array => ["published" => false]);
}
}
Route::get() and Route::post() register the HTTP method, path, and handler together. The handler is a PHP callable that Atom runs when the method and path match. A request to the same path with a different method can match a different handler. If the path matches but the method isn't registered, Atom returns 405 Method Not Allowed.
The example handlers return arrays, which Atom serializes as JSON.
Move the routes into a controller
As an API grows, a controller can keep related routes and handlers together. Atom's higher-level controller API lets you declare the shared prefix on the class and the HTTP method and remaining path on each handler.
<?php
namespace App\Controllers;
use Atom\Router\Attributes\Controller;
use Atom\Router\Attributes\Get;
use Atom\Router\Attributes\Post;
#[Controller("/api")]
final class ApiController
{
#[Get("health")]
public function health(): array
{
return ["status" => "ok"];
}
#[Post("articles/preview")]
public function preview(): array
{
return ["published" => false];
}
}
Import App\Controllers\ApiController in app/Application.php. Replace the two direct route declarations with one call in bootstrap():
protected function bootstrap(Injector $injector): void
{
Route::attach(ApiController::class);
}
Route::attach() reads the controller and method attributes and registers the same GET and POST routes.
Route API builds on the direct route version with grouping, parameter binding, and metadata. Controllers shows how to map routes with attributes.