composer require ekreative/health-check-bundle
config/app.php (Laravel 5.5+) or AppKernel.php (Symfony):
Ekreative\HealthCheckBundle\EkreativeHealthCheckBundle::class,
routes/web.php (Laravel) or config/routes.yaml (Symfony):
ekreative_health_check:
resource: "@EkreativeHealthCheckBundle/Resources/config/routes.xml"
/healthcheck in config/security.php (Laravel) or config/packages/security.yaml (Symfony):
firewalls:
healthcheck:
pattern: ^/healthcheck
security: false
/healthcheck to verify the endpoint returns a 200 OK with JSON response:
{
"status": "ok",
"database": true
}
Basic Health Check:
{ "status": "ok", "database": true }
Multi-Database Checks:
config/packages/ekreative_health_check.yaml:
ekreative_health_check:
doctrine:
- 'default'
- 'read_replica'
{ "status": "ok", "database": { "default": true, "read_replica": false } }
Custom Checks:
// src/Service/CustomHealthChecker.php
namespace App\Service;
use Ekreative\HealthCheckBundle\Checker\CheckerInterface;
class CustomHealthChecker implements CheckerInterface
{
public function check(): bool
{
// Your logic (e.g., API call, queue check)
return true;
}
public function getName(): string
{
return 'custom_service';
}
}
config/services.yaml:
services:
App\Service\CustomHealthChecker:
tags: ['ekreative.health_check.checker']
Integration with Monitoring:
- job_name: 'laravel'
static_configs:
- targets: ['your-app.com:80/healthcheck']
API Gateway/Load Balancer Health:
/healthcheck as a probe endpoint in AWS ALB, Nginx, or Kubernetes:
location /healthcheck {
proxy_pass http://backend;
health_check;
}
Security Misconfiguration:
/healthcheck exposes the endpoint to unauthorized access.security: false for the healthcheck firewall.Connection Failures:
false without context.storage/logs/laravel.log) for connection errors.Caching Issues:
// In a middleware or controller
return response()->json($data)->header('Cache-Control', 'no-cache');
Custom Checker Errors:
try-catch and log errors:
public function check(): bool {
try {
// Risky logic
return true;
} catch (\Exception $e) {
\Log::error('Health check failed: ' . $e->getMessage());
return false;
}
}
Response Customization:
// src/Http/Controllers/HealthCheckController.php
namespace App\Http\Controllers;
use Ekreative\HealthCheckBundle\Controller\HealthCheckController as BaseController;
class HealthCheckController extends BaseController
{
protected function getResponseData(): array
{
$data = parent::getResponseData();
$data['version'] = '1.0.0'; // Add custom fields
return $data;
}
}
Performance Optimization:
// In a custom checker
public function check(): bool {
$promises = [];
foreach ($this->connections as $connection) {
$promises[] = $this->checkConnection($connection);
}
return collect($promises)->every(fn ($result) => $result);
}
Environment-Specific Checks:
# config/packages/ekreative_health_check.yaml
enabled: '%env(bool:ENABLE_HEALTH_CHECKS)%'
Testing:
$this->app->instance(
Ekreative\HealthCheckBundle\Checker\CheckerInterface::class,
$this->createMock(Ekreative\HealthCheckBundle\Checker\CheckerInterface::class)
);
$response = $this->get('/healthcheck');
$response->assertStatus(200);
$response->assertJson(['status' => 'ok']);
Logging:
\Log::info('Health check result', [
'status' => $this->check(),
'checker' => $this->getName(),
]);
How can I help you explore Laravel packages today?