Alpha ZealPHP is early-stage and under active development. APIs may change between minor versions until v1.0. Feedback and bug reports welcome on GitHub.
API Index — Namespaces, Packages, Reports, Indices

HealthCheckMiddleware
in package
implements MiddlewareInterface

FinalYes

Health Check Middleware — short-circuits on configured paths and returns App::stats() as JSON. Designed for load-balancer probes, Kubernetes liveness/readiness, and monitoring agents.

Basic usage (intercepts /healthz):

$app->addMiddleware(new HealthCheckMiddleware());

Custom paths:

$app->addMiddleware(new HealthCheckMiddleware(
    paths: ['/healthz', '/readyz', '/_health']
));

With a custom readiness check (e.g., verify Redis/DB reachable):

$app->addMiddleware(new HealthCheckMiddleware(
    check: function(): ?string {
        try { \ZealPHP\Store::get('_ping', '_ping'); return null; }
        catch (\Throwable $e) { return 'store unreachable'; }
    }
));

Response shapes:

  • 200 {"status":"ok", "uptime_sec":123, ...} — healthy
  • 503 {"status":"unhealthy", "reason":"store unreachable", ...} — unhealthy

Table of Contents

Interfaces

MiddlewareInterface

Properties

$check  : callable(): Array|null
$paths  : array<int, string>

Methods

__construct()  : mixed
process()  : ResponseInterface
Intercept health-check paths and return a JSON status payload.

Properties

Methods

__construct()

public __construct([array<int, string> $paths = ['/healthz'] ][, callable(): Array|null $check = null ]) : mixed
Parameters
$paths : array<int, string> = ['/healthz']

URL paths to intercept (default ['/healthz']).

$check : callable(): Array|null = null

Returns null if healthy, an error string if not.

process()

Intercept health-check paths and return a JSON status payload.

public process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface

Non-matching paths are forwarded to $handler unchanged. When the optional $check callable returns a non-null string, the response status is 503 and the string is included as "reason" in the body.

Parameters
$request : ServerRequestInterface
$handler : RequestHandlerInterface
Return values
ResponseInterface
On this page