Swagger PHP API Documentation (executing php files to make json)

天大地大妈咪最大 提交于 2019-12-11 20:43:10

问题


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 for generating JSON file for Swagger-UI

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


回答1:


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.
    }
}

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).

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.



来源:https://stackoverflow.com/questions/25912073/swagger-php-api-documentation-executing-php-files-to-make-json

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!