Request Middleware

Request middleware can add context to a request or reject it before routing. Use a request ID to correlate logs and responses. Use a body size limit to keep oversized requests from reaching your handlers.

Global registration

Add both middleware classes to the application's existing middlewares() hook. Register request IDs first so a response returned by the body limiter can still carry an ID.

app/Application.php (excerpt)
use Atom\Http\MiddlewareRegistry;
use Atom\Http\RequestBodyLimitMiddleware;
use Atom\Http\RequestIdMiddleware;

protected function middlewares(MiddlewareRegistry $middlewares): void
{
    $middlewares
        ->add(RequestIdMiddleware::class)
        ->add(RequestBodyLimitMiddleware::class);
}

Request IDs

Atom makes the ID available in the downstream request and adds it to the response. By default it accepts a valid incoming ID. Set REQUEST_ID_TRUST_INCOMING=false if the application should generate its own IDs. Atom also generates an ID when the incoming value is missing or invalid.

.env
REQUEST_ID_HEADER_NAME=X-Request-Id
REQUEST_ID_TRUST_INCOMING=false
REQUEST_ID_MAX_LENGTH=128

Keep the maximum length at 32 or more so Atom's generated hexadecimal IDs fit. If the response already sets the configured header, the middleware preserves it.

Request body limits

Set the maximum size in bytes. This example allows up to 1 MiB. The default is 10 MiB, and 0 disables the limit.

.env
REQUEST_BODY_MAX_BYTES=1048576

Atom checks the declared Content-Length and the actual buffered body size. If either exceeds the limit, it returns 413 Content Too Large with the plain-text body Request body is too large. A malformed content length returns 400 Bad Request. In either case, the route handler does not run. With request IDs registered first, the response also includes the configured ID header.

The web server and PHP may enforce their own limits before Atom receives the request. Set those limits alongside the Atom limit for your deployment.