Installation
composer require bayzet/health-check
Register the service provider in config/app.php:
'providers' => [
// ...
Bayzet\HealthCheck\HealthCheckServiceProvider::class,
],
Basic Usage Define a health check in a service provider or boot method:
use Bayzet\HealthCheck\Facades\HealthCheck;
public function boot()
{
HealthCheck::addCheck('database', function () {
return DB::connection()->getPdo();
});
}
First Endpoint Add a route to trigger checks:
Route::get('/health', [HealthCheckController::class, 'check']);
(Note: The package doesn’t include a controller; you’ll need to create one.)
boot() or via events (e.g., registered).
HealthCheck::addCheck('cache', function () {
return Cache::getStore()->get('test');
});
database_connection, queue_worker).true/false for pass/fail.HealthCheck::addCheck('storage', function () {
if (!Storage::disk('local')->exists('health-check.txt')) {
throw new \RuntimeException('Storage check failed');
}
return true;
});
['tag' => 'database']).$results = HealthCheck::check(['tag' => 'database']);
public function handle($request, Closure $next)
{
if (!HealthCheck::check(['tag' => 'auth'])) {
abort(503);
}
return $next($request);
}
Artisan::call('health:check', ['--tag' => 'database']);
$results = HealthCheck::check();
return response()->json([
'status' => 'healthy',
'checks' => $results,
]);
No Built-in Controller/Route:
public function check()
{
$results = HealthCheck::check();
return response()->json($results);
}
No Persistent Storage:
No Built-in Scheduling:
$schedule->command('health:check')->everyMinute();
Exception Handling:
try-catch if needed.HealthCheck::getChecks() to verify registered checks.HealthCheck::addCheck('logs', function () {
\Log::info('Log check passed');
return true;
});
Custom Check Classes:
Extend the Bayzet\HealthCheck\Check class for reusable logic:
class DatabaseCheck extends Check
{
public function run()
{
return DB::connection()->getPdo() !== false;
}
}
Register via:
HealthCheck::addCheck(new DatabaseCheck('database'));
Custom Response Formatters:
Override the HealthCheck facade to modify output:
HealthCheck::extend(function ($app) {
$app->resolving('health.check', function ($check) {
// Modify response logic here
});
});
Dependency Injection: Inject dependencies into checks via closure binding:
HealthCheck::addCheck('mail', function ($mailer) {
return $mailer->mailer->getTransport();
})->with(\Illuminate\Mail\Mailer::class);
$results = Cache::remember('health_check_results', now()->addMinutes(5), function () {
return HealthCheck::check();
});
How can I help you explore Laravel packages today?