Skip to content

Your First Application

This guide takes you from an empty directory to a tested, running JSON API in a few minutes. Along the way you will see what makes PAM different from PHP-FPM: your application boots once, stays in memory, and serves every request from the same warm process.

You will use the api template, which creates a Nimbus application. Nimbus is PAM’s API-first framework: a Laravel-like structure, one routes file and controllers resolved through a container.

Before you start, install PAM and check that pam --version works.

Terminal window
pam init hello-api --template api
cd hello-api

pam init writes the project files and then installs the Composer dependencies with PAM’s embedded Composer. When it finishes you will see:

● PROJECT ONLINE
────────────────────────────────────────────────────────────
Preset API
Directory hello-api
Next cd hello-api && pam dev index.php

The interesting files are small. index.php loads the application and starts listening:

index.php
<?php
declare(strict_types=1);
/** @var \Nimbus\App $app */
$app = require __DIR__ . '/bootstrap/app.php';
$app->listen((int) (getenv('PAM_PORT') ?: 3000));

bootstrap/app.php builds the application without starting a listener, so tests and the bin/nimbus tinker shell can reuse it:

bootstrap/app.php
<?php
declare(strict_types=1);
use Nimbus\App;
$root = dirname(__DIR__);
$app = new App();
$app->routes($root . '/routes.php');
return $app;

See Directory Structure for every file the template creates.

Terminal window
pam dev index.php
PAM / DEV MATRIX hot reload active
────────────────────────────────────────────────────────────
● [ok] Watching /home/alice/hello-api
Ctrl+C stops the development runtime.
● READY Pam listening on http://127.0.0.1:3000

In another terminal, call the starter route:

Terminal window
curl -i http://127.0.0.1:3000/api/ping
HTTP/1.1 200 OK
content-type: application/json; charset=utf-8
content-length: 18
{"message":"pong"}

pam dev watches your PHP files, composer.json, composer.lock and .env. When one changes, it restarts the runtime and prints Change detected · reloading runtime. Stop it with Ctrl+C.

To use another port, set PAM_PORT for the process:

Terminal window
PAM_PORT=8080 pam dev index.php

Routes live in routes.php, which receives the router as $router. Add a route with a parameter:

routes.php
<?php
declare(strict_types=1);
use App\Controllers\GreetingController;
use App\Controllers\PingController;
use Nimbus\RouteRegistrar;
/** @var RouteRegistrar $router */
$router->get('api/ping', [PingController::class, 'show']);
$router->get('api/hello/{name}', [GreetingController::class, 'show']);

Generate the controller class with the API generator. It creates app/Controllers/GreetingController.php with an index action; replace it with a show action:

Terminal window
pam make:controller GreetingController
app/Controllers/GreetingController.php
<?php
declare(strict_types=1);
namespace App\Controllers;
use Pam\Http\Request;
use Pam\Http\Response;
final readonly class GreetingController
{
public function show(Request $request, Response $response): Response
{
$name = $request->route('name');
return $response->json([
'message' => "Hello, {$name}!",
'worker' => getmypid(),
]);
}
}

Save the files. pam dev reloads on its own, so you can call the new route right away:

Terminal window
curl http://127.0.0.1:3000/api/hello/Alice
{"message":"Hello, Alice!","worker":823692}

Unknown paths return a Problem Details response with a stable integer code:

Terminal window
curl -i http://127.0.0.1:3000/api/missing
HTTP/1.1 404 Not Found
content-type: application/problem+json; charset=utf-8
{"type":"https:\/\/pam.dev\/problems\/4","title":"Route not found.","status":404,"code":4}

Under PHP-FPM, every request starts from scratch. Under PAM, the worker that served the previous request is still alive. Add a closure route with a static counter to see it (put the two use lines with the other imports at the top of the file):

routes.php
use Pam\Http\Request;
use Pam\Http\Response;
$router->get('api/visits', static function (Request $request, Response $response): Response {
static $visits = 0;
return $response->json(['visits' => ++$visits]);
});
Terminal window
curl http://127.0.0.1:3000/api/visits # {"visits":1}
curl http://127.0.0.1:3000/api/visits # {"visits":2}
curl http://127.0.0.1:3000/api/visits # {"visits":3}

The counter survives between requests because the process does. That is where PAM’s speed comes from, and it is also the one rule you must keep in mind: anything you store in a static property, a global or a singleton is shared by every later request on that worker. Each worker also has its own copy, so a cluster of workers does not share the count. Read Memory & State before you keep per-user data anywhere but the request.

The template ships a PHPUnit suite that boots the application in memory, without opening a socket. Add a test for the new route:

tests/GreetingTest.php
<?php
declare(strict_types=1);
namespace Tests;
final class GreetingTest extends TestCase
{
public function test_it_greets_by_name(): void
{
$this->getJson('/api/hello/Alice')
->assertOk()
->assertJsonPath('message', 'Hello, Alice!');
}
public function test_unknown_routes_return_problem_details(): void
{
$this->getJson('/api/missing')
->assertNotFound()
->assertJsonPath('code', 4);
}
}

Run the suite inside PAM’s embedded PHP:

Terminal window
pam test
PHPUnit 12.5.45 by Sebastian Bergmann and contributors.
Runtime: PHP 8.5.13
Configuration: /home/alice/hello-api/phpunit.xml
... 3 / 3 (100%)
OK (3 tests, 9 assertions)

pam test picks PHPUnit or Pest automatically; force one with --phpunit or --pest, and pass any runner arguments after it, such as pam test --filter GreetingTest. The template also defines pam composer test as a shortcut.

pam dev runs one process with hot reload. In production you run several workers under a supervisor with pam start:

Terminal window
pam start index.php --workers 4 --admin-address 127.0.0.1:3010
Pam control plane listening on http://127.0.0.1:3010
● READY Pam listening on http://127.0.0.1:3000 worker=1
● READY Pam listening on http://127.0.0.1:3000 worker=2
...
Pam master 1068555 started 4 worker(s); send SIGHUP for zero-downtime reload

The control plane answers /live, /ready, /startup and /metrics. Send SIGHUP to the master for a zero-downtime reload and SIGTERM to drain in-flight requests and stop. Workers & Supervision explains the model in depth.

pam build copies the project, the pam binary and its PHP library into a new directory with an integrity manifest:

Terminal window
pam build
● PRODUCTION BUNDLE READY
────────────────────────────────────────────────────────────
Output /home/alice/hello-api/dist
Launch /home/alice/hello-api/dist/bin/pam-run

The output directory must not exist yet; choose another one with --output, and another entry file with --entry. Deployment explains what the bundle contains and how to run it, along with containers and the process manager.