编程

新的 Laravel 11 应用包括健康检查端点

328 2024-02-10 21:26:00

作为 Laravel 11 发布的一部分,新的应用包括一个健康 /up 端点。该路由是在新的 bootstrap/app.php 文件中通过传递健康相关参数来定义的,该参数默认在 Laravel 11 框架中定义:

Application::configure(basePath: dirname(__DIR__))
    ->withProviders()
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        // api: __DIR__.'/../routes/api.php',
        commands: __DIR__.'/../routes/console.php',
        // channels: __DIR__.'/../routes/channels.php',
        health: '/up', 
    )
    // ...

设置应用路由时,Laravel 框架定义健康状况路由,并发出 DiagnosingHealth 健康诊断事件:

use Illuminate\Foundation\Events\DiagnosingHealth;
 
// ...
 
if (is_string($health)) {
    Route::middleware('web')->get($health, function () {
        Event::dispatch(new DiagnosingHealth);
 
        return View::file(__DIR__.'/../resources/health-up.blade.php');
    });
}

该路由可使用默认 /up 端点进行配置,并在浏览器中返回动画“Application up”健康页面: