php 通过我自己的控制器查看空间健康检查JSON

wsxa1bj1  于 2023-05-16  发布在  PHP
关注(0)|答案(1)|浏览(133)

我在一个Laravel 10项目中工作,该项目使用了最新版本的Laravel Spatie Health检查包。我需要能够查看健康检查的JSON结果,而无需重新运行检查,也无需在控制器之间发出中间API请求。
这个包通过一个路由暴露了一个controller,但是我需要做一些处理,所以需要使用我自己的控制器。
如何通过下面的控制器函数访问JSON结果:

<?php

namespace App\Http\Controllers\Account;

use App\Http\Controllers\Controller;
use App\Http\Responses\ApiSuccessResponse;
use App\Http\Responses\ApiErrorResponse;
use App\Http\Responses\ApiValidationErrorResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Carbon\Carbon;
use Exception;

class HealthCheckStatusController extends Controller
{
    /**
     * Instantiate a new HealthCheckStatusController instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:sanctum');
    }

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        try {
            $schema = [
                'label' => 'Progressing',
                'state' => 'progressing',
                'textClass' => 'white',
                'bgClass' => 'info'
            ];

            // TODO: pull spatie health checks JSON here...

            return new ApiSuccessResponse($baseUrl);
        } catch (Exception $e) {
            return new ApiErrorResponse($e, [
                'message' => 'Unable to obtain health check status, please try again soon.'
            ]);
        }
    }
}
hgqdbh6s

hgqdbh6s1#

你可以使用与包相同的方法来获取最新的结果,如controller中所示,即使用ResultStore接口作为控制器函数的参数,并使用解析的对象来获取最新的结果:

<?php

namespace App\Http\Controllers\Account;

use App\Http\Controllers\Controller;
use App\Http\Responses\ApiSuccessResponse;
use App\Http\Responses\ApiErrorResponse;
use App\Http\Responses\ApiValidationErrorResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Spatie\Health\ResultStores\ResultStore;
use Carbon\Carbon;
use Exception;

class HealthCheckStatusController extends Controller
{
    /**
     * Instantiate a new HealthCheckStatusController instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:sanctum');
    }

    /**
     * Display a listing of the resource.
     */
    public function index(ResultStore $resultStore)
    {
        try {
            $schema = [
                'label' => 'Progressing',
                'state' => 'progressing',
                'textClass' => 'white',
                'bgClass' => 'info'
            ];

            $latestResults = $resultStore->latestResults()->toJson();

            //do what you need to do with these results...

            return new ApiSuccessResponse($baseUrl);
        } catch (Exception $e) {
            return new ApiErrorResponse($e, [
                'message' => 'Unable to obtain health check status, please try again soon.'
            ]);
        }
    }
}

这不会重新运行任何检查,因为它只是从存储结果的位置获取最新的结果,这将基于您如何配置health.php配置文件。

相关问题