# Add a Health Check Endpoint to a CodeIgniter 4 App

Yes. An agent adds a Health controller to your CodeIgniter 4 app with ResponseTrait JSON, a readiness route checking storage, cache and database, a test, in a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-health-checks/codeigniter/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the routing and test setup before it adds a controller. In an app created from codeigniter4/appstarter, `composer.json` requires `php ^8.2` and `codeigniter4/framework ^4.7`, with `phpunit ^10.5.16`, `fakerphp/faker` and `mikey179/vfsstream` for development, and a `test` script that runs `phpunit`. `app/Config/Routes.php` has one line, `$routes->get('/', 'Home::index')`, and `app/Config/Routing.php` sets `autoRoute` to false, so every endpoint must be declared. `app/Controllers/Home.php` extends `BaseController`, which is where helpers and shared services are loaded. `app/Config/Filters.php` keeps `forcehttps` and `pagecache` as required before-filters and `pagecache`, `performance` and `toolbar` after; `app/Config/Database.php` has an empty `database` in the `default` group and a MySQLi driver, so a fresh install has no database until `.env` sets one. `phpunit.dist.xml` bootstraps from `vendor/codeigniter4/framework/system/Test/bootstrap.php`, sets `app.baseURL` to `http://example.com/`, and leaves the `database.tests.*` variables commented out.

The controller follows the framework's API conventions rather than returning a view. The agent adds `app/Controllers/Health.php` extending `BaseController` and using `CodeIgniter\API\ResponseTrait`, so `index()` answers `GET /health` with `$this->respond(['status' => 'ok'])`, formatted by the JSON formatter `app/Config/Format.php` already lists with `JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES`. `ready()` answers `GET /health/ready` by running a list of checks and calling `respond()` with 200 when all pass or `fail()` with a 503 and the failing names when any does. Both are declared in `app/Config/Routes.php` under `$routes->get('health', 'Health::index')` and `$routes->get('health/ready', 'Health::ready')`, and the agent adds a `Config\Filters` entry so `pagecache` never caches them, since a cached readiness answer defeats the probe.

The checks come from what the starter actually configures. `WRITEPATH` must be writable because sessions, cache files and logs under `writable/` depend on it; the `cache()` service from `app/Config/Cache.php` gets a `save` and `get` of a short-lived key; and the database check runs only when `config('Database')->default['database']` is non-empty, calling `db_connect()->getVersion()` so an app with no database configured is still ready. Each check is a small class under `app/Libraries/Health/` with a `name()` and a `check(): bool`, and the list lives in `app/Config/Health.php` as a config the controller reads, so a team adds a queue or an external API check by adding a class name. Nothing is auto-routed and no filter is bypassed.

Tests use the framework's feature testing. `tests/feature/HealthTest.php` extends `CIUnitTestCase` with `FeatureTestTrait`, calls `$this->get('health')`, and asserts `assertStatus(200)`, `assertJSONFragment(['status' => 'ok'])` and the `application/json` content type; a second test swaps a failing check into the config with `Factories::injectMock` and asserts 503 under `health/ready`. The existing `tests/unit/HealthTest.php`, which only checks `APPPATH` and the `baseURL`, is left alone but mentioned in the pull request so nobody confuses the two. The agent runs `composer install` and `vendor/bin/phpunit` in its sandbox, where `phpunit.dist.xml` sets `cacheDirectory` to `build/.phpunit.cache`, and keeps the output.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add health check endpoints to this CodeIgniter 4 app. Read composer.json, phpunit.dist.xml, app/Config/Routes.php, app/Config/Routing.php, app/Config/Filters.php, app/Config/Database.php, app/Config/Cache.php, app/Config/Format.php, app/Controllers/BaseController.php, app/Controllers/Home.php, tests/unit/HealthTest.php and tests/README.md first. Add app/Controllers/Health.php using CodeIgniter\\API\\ResponseTrait with index() answering GET /health with {status: ok} and ready() answering GET /health/ready with 200 when every check passes and 503 listing the failing names otherwise. Put the checks under app/Libraries/Health as small classes with name() and check(), listed in a new app/Config/Health.php: WRITEPATH writable, a cache save and get, and a database getVersion only when the default group names a database. Declare both routes in app/Config/Routes.php and keep them out of the pagecache filter. Write tests/feature/HealthTest.php with FeatureTestTrait asserting the 200 JSON for /health and, with a failing check injected through Factories, the 503 for /health/ready. Run composer install and vendor/bin/phpunit, fix what fails, and open a pull request that notes the existing tests/unit/HealthTest.php is a different kind of check.",
  "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 `app/Controllers/Health.php`, `app/Config/Health.php`, three check classes under `app/Libraries/Health/` and `tests/feature/HealthTest.php`, and changes `app/Config/Routes.php` and `app/Config/Filters.php`. `Home`, the views and the database config are unchanged.

The description shows the JSON for `/health`, the 200 and a 503 for `/health/ready` with one check made to fail, and quotes the `phpunit` summary. The commands and their output are in the transcript at `GET /o/{org}/sessions/{id}/messages`.

Before merging, a reviewer decides whether `/health/ready` should require a token or stay internal to the load balancer, whether the database check should use the `tests` group under PHPUnit, and which checks to add for services the app will call.

## Run it on every CodeIgniter 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/Config/Database.php` and `app/Config/Cache.php`, so a service on MySQL with a Redis cache handler gets both checks, and one still on the file cache with no database gets the writable-path check and the cache round-trip 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.

## Why run it on Rush?

A health route in CodeIgniter is easy to get subtly wrong: a controller that returns a view instead of JSON, an endpoint the `pagecache` filter freezes, a database check that throws on a fresh install with no database, a test that hits the router with `autoRoute` off and a route that was never declared. The agent reads the routing, filter and database config, then runs the feature tests on a clean machine of its own before it opens anything.

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

## Frequently asked questions

### Why ResponseTrait instead of returning JSON from the controller directly?

`ResponseTrait` formats through `app/Config/Format.php`, sets the content type and gives named helpers for status codes, so the health endpoints answer the same way any API controller in the app will. It also keeps the controller small enough that the checks are the only logic in it.

### What happens on an install with no database configured?

The database check is skipped when the `default` group's `database` is empty, which is how the starter ships, so `/health/ready` reports ready. Once `.env` sets `database.default.database`, the check runs `getVersion()` on every call.

### Will the pagecache filter cache the health response?

No. The agent excludes `health` and `health/ready` from the `pagecache` alias in `app/Config/Filters.php`, because a cached readiness answer would keep reporting ready after a dependency fails.

### Isn't there already a HealthTest?

`tests/unit/HealthTest.php` checks that `APPPATH` exists and the `baseURL` is a valid URL; it is a sanity test of the install, not an HTTP endpoint. The new feature test lives under `tests/feature/` and the pull request explains the difference.

## More CodeIgniter jobs

- [Add i18n to a CodeIgniter 4 App](https://getrush.ai/run/add-i18n/codeigniter/)
- [Add JWT Authentication to a CodeIgniter 4 API](https://getrush.ai/run/add-jwt-authentication/codeigniter/)
- [Add PHPStan to a CodeIgniter 4 Application](https://getrush.ai/run/add-phpstan/codeigniter/)

## The same job on other stacks

- [Add Health Checks to a Django REST Framework API](https://getrush.ai/run/add-health-checks/django-rest-framework/)
- [Add Health Checks to a Docker Compose Setup](https://getrush.ai/run/add-health-checks/docker-compose/)
- [Add Health Checks to an Echo API](https://getrush.ai/run/add-health-checks/echo/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
