apiboxsym/health-check-bundle
Local Symfony bundle for ApiBoxSym that adds a simple health check endpoint. Exposes GET /health returning {"status":"ok"} for monitoring and infrastructure probes. Includes controller, bundle entrypoint, and tests; MIT licensed.
Installation Add the bundle to your Laravel project via Composer (assuming Symfony compatibility via Laravel's Symfony bridge or a standalone Symfony app):
composer require apiboxsym/health-check-bundle
Register the bundle in your config/bundles.php (Symfony) or manually bootstrap it in Laravel via a service provider.
Route Registration
The bundle auto-registers a /health endpoint. Verify by running:
php artisan route:list # (Laravel) or `php bin/console debug:router` (Symfony)
Expected output:
GET | /health | health.check.controller
First Use Case Test the endpoint locally:
curl http://localhost:8000/health
Expected response:
{"status":"ok"}
Basic Health Check
Use the /health endpoint for:
Customizing Responses Extend the default response by overriding the controller:
// app/Http/Controllers/HealthController.php (Laravel)
namespace App\Http\Controllers;
use ApiBoxSym\HealthCheckBundle\Controller\HealthController as BaseHealthController;
class HealthController extends BaseHealthController
{
public function check()
{
return response()->json([
'status' => 'ok',
'version' => '1.0.0',
'environment' => env('APP_ENV'),
]);
}
}
Update routes to point to your custom controller.
Dependency Checks Integrate with Laravel services (e.g., database, queue) by extending the bundle’s logic:
// Override the bundle’s service (Symfony)
services:
ApiBoxSym\HealthCheckBundle\Service\HealthChecker:
class: App\Service\CustomHealthChecker
arguments:
- '@database.connection'
- '@queue.worker'
API Versioning
Add versioned health checks (e.g., /health/v1):
// routes/web.php (Laravel)
Route::prefix('health')->group(function () {
Route::get('/v1', [HealthController::class, 'check']);
});
Namespace Conflicts The bundle assumes a Symfony environment. In Laravel, manually register routes/services to avoid autoloading issues:
// config/app.php (Laravel)
'providers' => [
// ...
ApiBoxSym\HealthCheckBundle\HealthCheckBundle::class,
];
Caching Headers The default endpoint lacks caching headers. Add them in Laravel:
return response()->json(['status' => 'ok'])
->header('Cache-Control', 'public, max-age=60');
Testing Edge Cases The bundle lacks built-in error handling. Test failure modes:
// tests/Feature/HealthCheckTest.php (Laravel)
public function test_health_check_fails_when_database_down()
{
\Mockery::mock('database.connection')->shouldReceive('getPdo')->andThrow(new \PDOException);
$response = $this->get('/health');
$response->assertStatus(503);
}
Monitoring Integration Use the endpoint with tools like:
/health with a custom exporter./health for uptime alerts.Custom Status Codes
Return 503 for degraded services:
if (!\DB::connection()->getPdo()) {
return response()->json(['status' => 'degraded'], 503);
}
Documentation Add OpenAPI/Swagger annotations for the endpoint:
/**
* @OA\Get(
* path="/health",
* summary="Health check endpoint",
* responses={
* 200={ "description": "Service is healthy" }
* }
* )
*/
Extending Checks Add modular checks via events (Symfony) or service providers (Laravel):
// Laravel Service Provider
public function register()
{
$this->app->singleton('health.checks', function () {
return collect([
'database' => fn() => \DB::connection()->getPdo() !== null,
'cache' => fn() => \Cache::store('file')->get('test') === 'test',
]);
});
}
How can I help you explore Laravel packages today?