Skip to content

PAM HTTP

pushinbr/pam-http is an HTTP application layer for the PAM runtime. Routes read like Express, while the application structure feels like Laravel: controllers resolved from a container, form requests that validate input, JSON resources that shape output, and Problem Details for every error.

index.php
$app = new Pam\App();
$app->get('/users/{id}', [UserController::class, 'show'])
->where('id', Pam\Api\RouteConstraint::Integer);
$app->listen(3000);

The package also ships the pieces a long-lived API needs: request-scoped dependency injection, rate limiting, signed bearer tokens, idempotency keys, response caching, the Arcano ORM integration, OpenAPI generation and an in-memory test client.

PAM HTTP is a Composer package; it runs inside the PAM runtime and is not a standalone server. Install PAM first.

Create a project from the PAM HTTP starter, which already wires PAM HTTP, Arcano, validation, resources and tests:

Terminal window
pam composer create-project pushinbr/pam-skeleton my-api
cd my-api

The package requires PHP 8.4 or newer and pushinbr/pam-contracts ^1.1. Optional companions:

Package Adds
pushinbr/arcano The Arcano ORM integration.
pushinbr/pam-http-psr PSR-7, PSR-15 and PSR-17 interoperability.
pushinbr/pam-socket Event-oriented WebSockets on the same listener.
pushinbr/pam-http-testing The in-memory test client for any PAM application.

Create an entry file that builds the application and starts the listener:

index.php
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use Pam\App;
use Pam\Http\Request;
use Pam\Http\Response;
$app = new App();
$app->get('/', static fn (Request $request, Response $response): Response =>
$response->json(['message' => 'Hello from PAM']));
$app->get('/users/{id}', static fn (Request $request, Response $response): Response =>
$response->json(['id' => (int) $request->route('id')]));
$app->listen((int) (getenv('PAM_PORT') ?: 3000));

Run it with the PAM CLI:

Terminal window
pam dev index.php # watches files and reloads on change
curl http://127.0.0.1:3000/users/42
# {"id":42}

In production, pam start index.php runs the same file as a supervised multi-worker cluster. See Production.

new App() accepts two optional arguments:

Argument Default Description
discoverPackages true Register service providers declared by installed Composer packages. See Package discovery.
container null A pre-built Pam\Api\Container\Container. A new one is created when omitted.

Pass discoverPackages: false in tests and console scripts when you want every provider registered explicitly.

listen(int $port, string $host = '127.0.0.1', array $options = []) boots the providers, freezes the configuration and hands the request handler to the PAM transport. The third argument is passed to the runtime unchanged, so every listener option works here:

index.php
$app->listen(3000, '0.0.0.0', [
'maxBodyBytes' => 8 * 1024 * 1024,
'requestTimeoutMs' => 15_000,
'trustedProxies' => ['10.0.0.0/8'],
]);

Configuration reads environment variables once, validates their types and fails during boot when a required value is missing or malformed:

index.php
use Pam\Api\Config\ConfigDefinition;
use Pam\Api\Config\Configuration;
use Pam\Api\Config\ConfigType;
$config = Configuration::fromEnvironment([
new ConfigDefinition('app.port', 'PAM_PORT', ConfigType::Integer, required: false, default: 3000),
new ConfigDefinition('auth.secret', 'AUTH_SECRET', sensitive: true),
]);
$app->listen($config->integer('app.port'));

ConfigDefinition takes a key, the environment variable, a ConfigType (String = 1, Integer = 2, Boolean = 3, Float = 4; String by default), whether it is required (default true), a default and a sensitive flag. Read values with string(), integer(), boolean() and float().

When validation fails, ConfigurationException lists every problem in its errors property; messages name the variable and the expected type but never echo the supplied value. Use $config->redacted() for diagnostics: it returns every value with sensitive ones masked. Configuration::fromArray($definitions, $environment) builds the same object from an array, which is convenient in tests.

Routes, middleware, providers, observers and the error handler can only be registered before the application handles its first request (or before listen() / boot()). After that, any change throws a LogicException instead of silently mutating a pipeline that is shared by every request in the worker.

PAM keeps your application in memory between requests. The App, its routes and its singletons are built once per worker and reused. PAM HTTP gives you two tools to keep requests isolated:

  • Scoped bindings. The container creates scoped values once per request and discards them when the request ends, even when it throws. See Container lifetimes.
  • Per-request state from middleware. Authentication, tenants and deadlines are stored as scoped instances, never on singletons.