php Laravel l5-未找到Laravel l5-swagger api-docs.json文件

c2e8gylq  于 2023-10-15  发布在  PHP
关注(0)|答案(1)|浏览(147)

我安装了

composer require "darkaonline/l5-swagger:5.6.*",

我有一个来自swagger的api-docs.json,我把它添加到storage/api-docs/api-docs.json。
但当我去

http://127.0.0.1:8000/api/documentation

我出错了

Fetch error Not Found http://127.0.0.1:8000/docs/api-docs.json

我试图运行

php artisan l5-swagger:generate

但逻辑上会返回一个错误

Required @OA\Info() not found

因为它希望我手动输入类中的所有API信息,但我的API非常大,所以重写它需要很长时间。
darkaonline/l5-swagger可以从存储中读取我的api-docs.json文件吗?

bmp9r5qi

bmp9r5qi1#

您需要添加错误中提到的Info
一个简单而强大的解决方案是将其添加到App\Http\Controller::class

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use OpenApi\Attributes as OA;


#[OA\Info(title: "My First API", version: "0.1",)]

class Controller extends BaseController
{
    use AuthorizesRequests, ValidatesRequests;
}

这将确保Info也被添加到所有请求中。
然后,您可以轻松地添加PathItem,例如App\Http\Controller\Api\HeartbeatController::class

<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use OpenApi\Attributes as OA;

class HeartbeatController extends Controller
{
    #[OA\Get(
        path: '/heartbeat',
        responses: [
            new OA\Response(response: '200', description: 'API is running', content: new OA\JsonContent(example: ['status' => 'ok', 'message' => 'API is running']))
        ],
    )]
    public function __invoke(): \Illuminate\Http\JsonResponse
    {
        return response()->json([
            'status' => 'ok',
            'message' => 'API is running',
        ]);
    }
}

相关问题