问题
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