Lewati ke konten
Rama's logo Qisthi Ramadhani
Go back

Building Health Check Endpoints in Modern PHP Frameworks: Hyperf, Hypervel, and Webman

Health check endpoints are essential for maintaining the reliability and observability of web applications. They enable automated systems and developers to verify that an application and its dependencies are running as expected. In this comprehensive guide, I’ll break down the implementation of /up health check endpoints in three popular async PHP frameworks—Hyperf, Hypervel, and Webman—based on recent commits from my open-source repositories.


Why Health Check Endpoints Matter

A health check endpoint is a lightweight HTTP route (commonly /up or /health) that returns a status indicating whether the application is operational. These endpoints are widely used by cloud platforms, load balancers, Kubernetes probes, and monitoring tools to automate service health verification. A robust health check can also include checks for database connectivity, cache systems, and other critical dependencies.


1. Hyperf: Coroutine-Powered Health Checks

Commit Reference: e09a5d2

Hyperf is a high-performance coroutine framework for PHP, designed for building scalable, cloud-native applications. Here’s how the /up endpoint is implemented:

Controller Implementation

namespace App\Controller;

use Hyperf\View\RenderInterface;

class HealthUpController extends AbstractController
{
    public function __invoke(RenderInterface $render)
    {
        $checks = [];
        $exception = null;

        try {
            // Example: \Hyperf\DbConnection\Db::connection()->select('SELECT 1');
            // $checks['db'] = 'UP';

            // Example: $this->cache->set('health-check', 'up');
            // $checks['cache'] = 'UP';
        } catch (\Throwable $e) {
            $exception = $e->getMessage();
        }

        return $render->render('health-up', compact('checks', 'exception'));
    }
}

Route Registration

Add this to config/routes.php:

use App\Controller\HealthUpController;

Router::get('/up', HealthUpController::class);

Blade View

The endpoint renders a Blade template (health-up.blade.php) with a status indicator. If $exception is set, the indicator turns red and displays an error message; otherwise, it shows the app is up.

Key Points:


2. Hypervel: Laravel-Style Health Checks for Coroutine PHP

Commit Reference: 7c835b6

Hypervel brings Laravel-like developer experience to coroutine PHP. The /up endpoint implementation closely mirrors Laravel conventions.

Controller Implementation

namespace App\Http\Controllers;

class HealthUpController
{
    public function __invoke()
    {
        $checks = [];
        $exception = null;

        try {
            // \Hypervel\Support\Facades\DB::connection()->select('SELECT 1');
            // $checks['db'] = 'UP';

            // \Hypervel\Support\Facades\Cache::put('health-check', 'OK', 60);
            // $checks['cache'] = 'UP';
        } catch (\Throwable $e) {
            if (app()->hasDebugModeEnabled()) {
                throw $e;
            }
            report($e);
            $exception = $e->getMessage();
        }

        return view('health-up', compact('checks', 'exception'));
    }
}

Route Registration

In routes/web.php:

use App\Http\Controllers\HealthUpController;

Route::get('/up', HealthUpController::class);

Blade View

The view (health-up.blade.php) displays a green or red status indicator, with a message reflecting the health of the application.

Key Points:


3. Webman: Fast, Minimalist Health Endpoint

Commit Reference: c2aaeae

Webman is a minimalist async PHP framework built on Workerman. The health check implementation here is straightforward and efficient.

Controller Implementation

namespace app\controller;

class HealthUpController
{
    public function index()
    {
        $exception = null;

        try {
            // Add DB or Redis checks here as needed.
        } catch (\Throwable $e) {
            $exception = $e->getMessage();
        }

        $status = $exception ? 'status-down' : null;
        $message = $exception ? 'experiencing problems' : 'up';

        return <<<HTML
            <!DOCTYPE html>
            <html lang="en">
            <head>
                <meta charset="utf-8">
                <meta name="viewport" content="width=device-width, initial-scale=1">
                <title>Webman</title>
                <!-- Fonts and Styles omitted for brevity -->
            </head>
            <body>
                <div>
                    <div class="status-indicator $status"></div>
                    <h2>Application $message</h2>
                    <p>HTTP request received.</p>
                </div>
            </body>
            </html>
        HTML;
    }
}

Route Registration

In config/route.php:

use app\controller\HealthUpController;

Route::get('/up', [HealthUpController::class, 'index']);

Key Points:


Common Patterns & Best Practices


Conclusion

Implementing a /up health check endpoint is a best practice for any modern PHP application, especially those running in cloud or containerized environments. Whether you use Hyperf, Hypervel, or Webman, the patterns above provide a solid foundation for observability, reliability, and rapid troubleshooting.

For source code and further exploration, see the referenced commits for each framework. Feel free to extend these endpoints to cover more dependencies as your application grows.


Share this post on:
LLM-friendly version:
Open in ChatGPT Open in Claude

Related Posts


Previous Post
Benchmarking PHP Health Endpoints with PestPHP Stress Test
Next Post
Unlocking Indonesian Expertise: A Deep Dive into Malaka Project's 'Kelas Pakar'