0

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 don't know how to get through this problem can anyone help, please? Here is the link I study but to no worth.

https://github.com/zircote/swagger-php

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 will be very thankful to him thanks.

Faizan
  • 86
  • 13
Raza
  • 1
  • 1
  • 2

1 Answers1

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

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.

Community
  • 1
  • 1
Mark
  • 5,437
  • 2
  • 19
  • 29