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

use*_*197 3 php json autodoc swagger

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

我试过这个链接: - https://github.com/zircote/swagger-php

此外,我尝试在http://zircote.com/swagger-php/installation.html上处理文档

但我的运气不好,我无法实现它.

我能够正确安装composer.此外,正确安装了Swagger-PHP捆绑包.

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

所以,如果有人在处理它,请帮助!

提前致谢 !!

Tom*_*ane 10

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

/**
* @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
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

控制器示例:

/**
 * @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
 {
     // ...
 }
Run Code Online (Sandbox Code Playgroud)

然后在控制台中:

php swagger.phar ./your-code-source/ -o ./directory-for-output-files
Run Code Online (Sandbox Code Playgroud)

然后在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
Run Code Online (Sandbox Code Playgroud)

编辑:Swagger 2.0 在GitHub上有很好的规范

顺便说一下,考虑使用Swagger Editor来创建在Swagger UI中使用的api规范文件(json/yaml).导致php文件中的内联SWG文档非常难看,并且您在IDE中没有自动完成支持.

  • 如果您可以在注释中使用Swagger Editor yaml,那么内联文档是不是很难看,这不是很酷吗? (2认同)