Swagger PHP API 文档(执行 php 文件生成 json) [英] Swagger PHP API Documentation (executing php files to make json)

查看:54
本文介绍了Swagger PHP API 文档(执行 php 文件生成 json)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PHP swagger 制作 Restful API 文档,我之前所做的是更改了 json 以解决问题,现在我知道我们可以通过制作 PHP 文件并使用 swagger 符号来制作 json.我确实检查了 Pet.php 示例并获得了代码,但我不知道如何执行该文件以获取可以与 Swagger UI 链接的 json api 文档.我阅读了文档,但它是如此令人困惑,我不知道如何解决这个问题,任何人都可以帮忙.这是我研究过但没有价值的链接.

I am trying to make the Restful API documentation in PHP swagger, what i did before is that i changed the json to work out, now i know we can make the json by making PHP files and using swagger notation. I did check the Pet.php example and i get the code but i don't know how to execute the file to get the json api documentation which i can link with my Swagger UI. I read the documentation but it is so confusing and i dont know how to get through this problem can anyone help please. Here are the link i study but to no worth.

http://zircote.com/swagger-php/using_swagger.html

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

谁能一步一步告诉我如何在json中生成api文档.我非常感谢他谢谢

can anyone tell me step by step how to generate the api documentation in json. I be very thankful to him thanks

推荐答案

在 swagger-php 2.0 中有两种方法可以实现.

There are two way to achieve this in swagger-php 2.0.

我.第一个解决方案是创建一个控制器或脚本,它将为每个请求生成文档.在您希望快速查看更改结果的开发环境中,这是一个很好的解决方案.

I. The first solution is to create a controller or script which will generate the documentation on each request. This is a good solution in a development environment where you want to see quickly the outcome of your changes.

这是执行此操作的控制器示例.

Here is an example of a controller which does this.

<?php
namespace Controllers;

use Swagger\Annotations as SWG;
use Swagger;

/**
 * @SWG\Swagger(
 *      basePath="/path/to/opration/",
 *      produces={"application/json"},
 *      swagger="2.0",
 *      @SWG\Info(
 *          version="1.0.0",
 *          title="My API"
 *      )
 * )
 *
 */
class Documentation {

    const API_PATH = "path/to/my/documented/files/";

    public function show(){
        $swagger = Swagger\scan(self::API_PATH);

        return json_enconde($swagger); //you can echo this in the calling script.
    }
}

注意: 上面的例子假设你用 Composer 安装了 Swagger-php 并且调用脚本包含 Composer 生成的自动加载文件(通常称为:vendor/autoload.php).

Note: The example above assumes you installed Swagger-php with Composer and that the calling script include the composer generated autoload file (usually called: vendor/autoload.php).

二.此处描述了由生成静态 json API 文档组成的第一个解决方案:https://stackoverflow.com/a/21380432/2853903一个>

II. The first solution consisting of generating a static json API documentation is described here: https://stackoverflow.com/a/21380432/2853903

此解决方案推荐用于生产部署,您不希望针对每个请求重新生成文档.

This solution recommended for production deployment, where you would not want to regenerate the documentation on every request.

这篇关于Swagger PHP API 文档(执行 php 文件生成 json)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆