php - 用于为 Swagger-UI 生成 JSON 文件的 Swagger-PHP

标签 php json swagger autodoc

我正在尝试使用 Swagger-PHP 生成 JSON 文件,以便我可以将它与 Swagger-UI 一起用于自动文档。

我尝试了链接:- https://github.com/zircote/swagger-php

我还尝试使用 http://zircote.com/swagger-php/installation.html 上的文档解决问题

但是运气不好,我无法实现它。

我能够正确安装 Composer 。 Swagger-PHP 包也已正确安装。

但问题是我无法使用/理解他们提供的测试示例。

所以,如果有人解决了这个问题,请帮忙!!

提前致谢!!

最佳答案

你只需在你的代码中添加注释,模型示例:

/**
* @SWG\Model(
* id="vps",
* required="['type', 'hostname']",
*  @SWG\Property(name="hostname", type="string"),
*  @SWG\Property(name="label", type="string"),
*  @SWG\Property(name="type", type="string", enum="['vps', 'dedicated']")
* )
*/
class HostVps extends Host implements ResourceInterface
{
    // ...
}

Controller 示例:

/**
 * @SWG\Resource(
 *  basePath="http://skyapi.dev",
 *  resourcePath="/vps",
 *  @SWG\Api(
 *   path="/vps",
 *   @SWG\Operation(
 *    method="GET",
 *    type="array",
 *    summary="Fetch vps lists",
 *    nickname="vps/index",
 *    @SWG\Parameter(
 *     name="expand",
 *     description="Models to expand",
 *     paramType="query",
 *     type="string",
 *     defaultValue="vps,os_template"
 *    )
 *   )
 *  )
 * )
 */
 class VpsController extends Controller
 {
     // ...
 }

然后在控制台中:

php swagger.phar ./your-code-source/ -o ./directory-for-output-files

然后在 Swagger UI 中链接生成的文件。 这是帮助吗?

顺便说一句,这个文档:http://zircote.com/swagger-php/annotations.html不完整。最好依靠解析器错误,例如:

php swagger.phar ./skynode-api/api/ -o ./foo
Swagger-PHP 0.9.0
-----------------
[INFO] Skipping unsupported property: "foo" for @Swagger\Annotations\Property, expecting "name", "description", "type", "format", "items", "uniqueItems", "required", "minimum", "maximum", "enum", "defaultValue", "_partialId", "_partials" in HostVps in /home/kane/some-dir/some-file.php on line 3

编辑:Swagger 2.0 有相当不错的规范 ​​on GitHub

顺便说一句,考虑使用Swagger Editor创建要在 Swagger UI 中使用的 api 规范文件 (json/yaml)。因为 php 文件中的内联 SWG 文档很难看,而且您在 IDE 中没有自动完成支持。

关于php - 用于为 Swagger-UI 生成 JSON 文件的 Swagger-PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16359223/

相关文章:

php - 如何创建 JSON 对象

Swagger UI 错误 : Unable to fetch API Listing

php - 数据未插入数据库 PHP MySQL

javascript - 在调用 PHP 函数的 AJAX 请求后提交表单时重定向页面

php - 如何更正此 SQL 查询的 PHP 代码?

azure - 在隐式流的情况下是否可以在 JWT token 中包含组?

java - 一起使用 Spring Boot、QueryDSL 和 Springfox Swagger - Guava 版本不匹配

php - 如何从图像结果列表中选择一张图像?

php - 使用 PHP 在 JSON 条目之间插入记录

.net - Swagger .NETCORE 无法读取 json