All jobs

Add a Health Check Endpoint to a Slim 4 App

Yes. An agent adds a /health action in the Action pattern your Slim 4 app already uses, a readiness check list wired through PHP-DI, a PHPUnit test, and opens a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads the request path before it adds a route. In an app built from slimphp/Slim-Skeleton, composer.json requires php ^7.4 || ^8.0, slim/slim ^4.12, slim/psr7 ^1.6, php-di/php-di ^6.4 and monolog/monolog ^2.9, with phpunit ^9.6, phpstan ^1.10 and squizlabs/php_codesniffer ^3.9 for development. public/index.php builds the container from app/settings.php, app/dependencies.php and app/repositories.php, adds SessionMiddleware, registers app/routes.php, then adds routing, body-parsing and error middleware with a custom HttpErrorHandler. app/routes.php has an OPTIONS /{routes:.*} handler, GET / writing a hello-world string, and a /users group whose actions extend App\Application\Actions\Action. That base class gives every action respondWithData(), which wraps the data in ActionPayload as {"statusCode": …, "data": …} with JSON_PRETTY_PRINT, and converts DomainRecordNotFoundException into a 404.

A health endpoint in this codebase is one more action, not a closure. The agent adds src/Application/Actions/Health/HealthAction.php extending Action, registered in app/routes.php as GET /health outside the /users group, that returns respondWithData(['status' => 'ok'], 200), so the body has the same statusCode and data envelope the user actions return and the existing ActionPayload test expectations stay valid. For readiness it adds src/Application/Health/HealthCheck.php, an interface with name() and check(): bool, and src/Application/Health/HealthChecks.php, a collection the container builds in app/dependencies.php the same way it builds LoggerInterface. The one check shipped is LogPathWritable, which reads the logger path from SettingsInterface and reports whether the directory is writable, because app/settings.php writes to logs/app.log outside Docker and to php://stdout when the docker environment variable is set. GET /health/ready runs every registered check and answers 200 when all pass or 503 with the failing names, through respondWithData so the shape stays the same.

Two details in the skeleton decide the plumbing. SessionMiddleware calls session_start() whenever an Authorization header is present, so the agent documents that probes must not send one and keeps the health routes free of any attribute lookup. The repository layer is InMemoryUserRepository, so there is no database to ping; the interface exists so that when a PDO connection lands in app/repositories.php, a DatabaseReachable check is one class. Everything stays PHP 7.4-compatible, no enum, no readonly, because .github/workflows/tests.yml runs the suite on 7.4, 8.0, 8.1 and 8.2 and runs phpcs and phpstan on 8.1.

The test follows tests/Application/Actions/User/ListUserActionTest.php: tests/Application/Actions/Health/HealthActionTest.php extends Tests\TestCase, calls getAppInstance(), builds a request with createRequest('GET', '/health') and compares the body to a serialized ActionPayload(200, ['status' => 'ok']). A second test replaces HealthChecks in the container with a prophecy whose single check fails and asserts the 503 and the failing name under /health/ready. The agent runs composer install, vendor/bin/phpunit, vendor/bin/phpcs against phpcs.xml (PSR-12 plus no long arrays) and vendor/bin/phpstan at level 4 from phpstan.neon.dist, and keeps the output.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add health check endpoints to this Slim 4 app. Read composer.json, public/index.php, app/routes.php, app/settings.php, app/dependencies.php, app/repositories.php, app/middleware.php, src/Application/Actions/Action.php, src/Application/Actions/ActionPayload.php, src/Application/Middleware/SessionMiddleware.php, tests/TestCase.php, tests/Application/Actions/User/ListUserActionTest.php, phpcs.xml, phpstan.neon.dist and .github/workflows/tests.yml first. Add a HealthAction under src/Application/Actions/Health that extends Action and answers GET /health with respondWithData(['status' => 'ok']). Add a HealthCheck interface and a HealthChecks collection under src/Application/Health, registered in app/dependencies.php, with one check that the logger path from settings is writable, and a GET /health/ready action that returns 200 when every check passes and 503 with the failing names otherwise. Keep everything PHP 7.4-compatible because the workflow tests 7.4 through 8.2. Write PHPUnit tests in tests/Application/Actions/Health that use getAppInstance and createRequest, one asserting the /health payload and one replacing HealthChecks in the container with a failing check and asserting the 503. Run composer install, vendor/bin/phpunit, vendor/bin/phpcs and vendor/bin/phpstan, fix what they report, and open a pull request that documents that probes must not send an Authorization header because of SessionMiddleware.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

What comes back is a pull request that adds HealthAction.php, ReadinessAction.php, the HealthCheck interface, HealthChecks, LogPathWritable and two test classes, and changes app/routes.php and app/dependencies.php. No existing action, middleware or setting is modified.

The description shows curl output for /health and /health/ready, including the 503 body when the log directory is made read-only, and quotes the phpunit, phpcs and phpstan results. The transcript at GET /o/{org}/sessions/{id}/messages keeps every command and its output.

Before merging, a reviewer decides whether /health/ready should be reachable from outside the load balancer, whether a check for the PHP-DI compiled container cache under var/cache belongs in the list once compilation is turned on in public/index.php, and which checks to add when a database arrives.

Run it on every Slim service deployed without a probe

A platform team runs this once per service from its own server: one POST /o/{org}/sessions per repository with the repository in resources and this prompt as input. Each run reads its own app/dependencies.php, so a service with a PDO definition gets a database check and one with a Redis client gets a PING, while a skeleton like this one gets the writable-log check alone.

Send a request_id, a UUID v5 of the repository's full name, so a retried call returns the run it already started. The key's webhook_url reports when each run the key starts finishes, with session_url for the pull request link.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

A health route is ten lines, and the details are where it goes wrong: a closure that skips the error handler, a payload shape that differs from every other response, a probe that triggers session_start() because a header slipped in, a PHP 8 construct that breaks the 7.4 job. The agent reads the action base class, the middleware and the CI matrix, then runs the suite, the sniffer and the static analyser on a clean machine of its own before it opens anything.

The run keeps every command and result, so a reviewer sees the 200 and the 503 and the three green tools in the pull request rather than reproducing them, and several Slim services can be done at once, each in its own sandbox.

Frequently asked questions

Why an Action class instead of a closure in routes.php?

The skeleton's user routes are classes that extend Action, which gives them logging, respondWithData() and the ActionPayload envelope. A closure would answer with a different body shape and bypass that base class, so the health routes follow the pattern the rest of the app already uses.

What does readiness check when there is no database?

Only that the logger path from app/settings.php is writable, since the skeleton stores users in memory. The HealthCheck interface is there so a database or cache check is one class registered in app/dependencies.php when you add the dependency.

Will the probe start a PHP session?

Not unless it sends an Authorization header: SessionMiddleware calls session_start() only in that case. The pull request documents that probes send no auth header, and the test requests use only HTTP_ACCEPT.

Does it stay compatible with PHP 7.4?

Yes. composer.json allows ^7.4 || ^8.0 and the workflow tests 7.4, so the new classes use typed properties and nothing newer. phpstan at level 4 and phpcs with PSR-12 run as they do in CI.

More Slim jobs

The same job on other stacks

Keep reading