首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swagger-PHP,用于为Swagger-UI生成JSON文件

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

Stack Overflow用户
提问于 2013-05-03 20:42:20
回答 2查看 18.8K关注 0票数 4

我正在尝试使用Swagger-PHP来生成JSON文件,这样我就可以将它与Swagger-UI一起用于汽车文档。

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

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

但不幸的是,我无法实现它。

我能够正确地安装composer。此外,Swagger-PHP包也已正确安装。

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

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

提前感谢!!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-27 20:04:55

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

代码语言:javascript
复制
/**
* @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
{
    // ...
}

控制器示例:

代码语言:javascript
复制
/**
 * @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
 {
     // ...
 }

然后在控制台中:

代码语言:javascript
复制
php swagger.phar ./your-code-source/ -o ./directory-for-output-files

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

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

代码语言:javascript
复制
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中也没有自动完成功能。

票数 11
EN

Stack Overflow用户

发布于 2016-09-01 09:11:06

我最近一直在努力解决这个问题,看到了一些不同之处,并设法让它正常工作,并希望或许能为其他人提供捷径:

代码语言:javascript
复制
// In the controller

/**
 * (other swagger stuff like @SWG\Put, etc...)
 *   @SWG\Parameter(name="type",in="path",description="project|task",
 *                  required=true, type="array",             
 *                  @SWG\Items(type="string"), 
 *                  default="project",
 *                  enum={"project", "task"}),
 * (next @SWG\Parameter or closing paran, etc)
 */

我之前在模型中使用枚举和@SWG\Items引用获得了它,但没有保存该代码(我只是在胡闹),而且我无法找回它。我甚至看到我之前提升了问题和接受的答案!但以上是我现在唯一能让它工作的方法,而且总比什么都没有好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16359223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档