Esc
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, ...}— healthy503{"status":"unhealthy", "reason":"store unreachable", ...}— unhealthy
Table of Contents
Interfaces
- MiddlewareInterface
Properties
Methods
- __construct() : mixed
- process() : ResponseInterface
- Intercept health-check paths and return a JSON status payload.
Properties
$check
private
callable(): Array|null
$check
$paths
private
array<int, string>
$paths
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
nullif 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