thinkphp combination of swagger automatically generated interface documentation

1, swagger installation

Step 1: Install distal swagger-ui

Go here to download https://github.com/swagger-api/swagger-ui

After the download is complete, the folder into the root directory of your website above, for example, I was on my wamp following www directory.

Then find dist directory, open index.html to them that string url into their own such ashttp://localhost/tp/public/swagger.json

Note that the url is swagger.json behind the path;

If you want to add Chinese support in the index.html

<script src='lang/translator.js' type='text/javascript'>

</script><script src='lang/zh-cn.js' type='text/javascript'></script>

Then open the URL input http://localhost/swagger-ui/dist/index.htmlyou can see the front-end interface, it should not be content, because no generation swagger.json, generate good URL after you set it played a role. swagger.json I was on the swagger-docs directory of the tp framework, specific path depends on your own particular will be mentioned below, do not panic O (∩_∩) O ~.

Step 2: Install swagger-php backend

Into your project directory execute the following command:

composer require zircote/swagger-php

After the installation is complete it will generate a prompt zircote the vendor you tp project in the Components folder, install the plug explained that it had succeeded.

The third step: generating swagger.json file

方法1、直接在命令行中输入:
php D:\Program\www\tp\vendor\zircote\swagger-php\bin\swagger D:\Program\www\tp\application\app\controller\ -o D:\Program\www\tp\public
Note: The first path is the path after you successfully install components; 
the second is the path you want to generate all the way with a swagger comments php files in this directory, all the comments generated api documentation; 
the third is the path you store the generated path swagger.json

Method 2, a method of generating write controller swagger.json:

If every time we modify the api, but also manually perform the third step of the code, somewhat cumbersome, then we write a method in the controller, every time you visit swagger-ui is performed automatically, then jump to the front swagger interface in.

The following is a method which the controller


$path = '../application'; //你想要哪个文件夹下面的注释生成对应的API文档
$swagger = \Swagger\scan($path);
//header('Content-Type: application/json');
//echo $swagger;
$swagger_json_path = $path.'/swagger-docs/swagger.json';
$res = file_put_contents($swagger_path, $swagger);
if ($res == true) {
$this->redirect('http://localhost/swagger-ui/dist/index.html');
}

 

Step Four: Write swagger comment

Notes written controller

/**
 * @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
{
    // ...
}

This is just a simple example of a specific written comments at your own Baidu

2, swagger Comment Use

Reference this (write a more comprehensive): https: //learnku.com/laravel/t/7430/how-to-write-api-documents-based-on-swagger-php#747b67

Clue yet to be seen, then read tomorrow

 

Guess you like

Origin www.cnblogs.com/jcydd/p/11455989.html