thinkphp6+swagger-php Configuration Management Interface Document

Keywords: PHP JSON github Javascript

swagger2 was upgraded to 3 and renamed OpenAPI Spec, with some changes in all partial annotations, which are configured here with thinkphp6+swagger-php3.0

1. The front-end part git or dowload a swagger-ui to be able to access the service directory, such as where my nginx configuration points to the thinkphp6 root directory public, so download a swagger-ui to the root directory, swagger-ui download address https://github.com/swagger-api/swagger-ui

Find the dist directory, open index.html and change the URL to your own server url. Here I'll take the local configuration as an example:

If you want to support adding Chinese in index.html

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

The original project page address is: http://127.0.0.1:8806 Now the ui address of the front end of the interface is: http://127.0.0.1:8806/swagger-ui/dist/index.html At this time, because swagger.json is not configured to display only the header, interface details cannot be displayed

2. Install the swagger-php backend, execute under the general directory of the thinkphp6 framework, and composer installs the swagger-php plug-in.(It is best to use the composer management plug-in in case composer cannot be used and you can try to install it manually)

composer require zircote/swagger-php

Note that the installation is now using the latest version of 3.0

3. Here, swagger customizes the annotations, and swagger-php generates the interface configuration file for swagger.json.Generation method:

  • Command Line Generation

    $> php /usr/local/var/www/vendor/zircote/swagger-php/bin/openapi /usr/local/var/www/app/api/controller -o /usr/local/var/www/public/uploads

Where:
/usr/local/var/www/vendor/zircote/swagger-php/bin/openapi Main command to generate json files for the swagger-php plug-in
/usr/local/var/www/app/api/controller is the interface directory, which is scanned by the main command in turn to generate the corresponding json configuration code
/usr/local/var/www/public/uploads is the directory where swagger.json stores files

  • Automatically generate swagger.json from controller code

    public function apidoc(){
    // $RootDir = $_SERVER['DOCUMENT_ROOT'];

        $path ='../app/api/controller'; //Which folder do you want comments under to generate the corresponding API document
        $swagger = \OpenApi\scan($path);
        header('Content-Type: application/x-yaml');
        // var_dump($swagger);
        $swagger_json_path = './uploads/swagger.json';
    
        $res = file_put_contents($swagger_json_path,$swagger-&gt;toYaml());
        if ($res == true) {
            return redirect('http://127.0.0.1:8806/swagger-ui/dist/index.html');
        }
    }

Automatically generating swagger.json automatically jumps to the initial configuration to the swagger-ui front-end address. We can try to create a new swagger.php in the controller directory, as follows:

/**

  • @OA\Swagger(
  • schemes={"http"},
  • host="127.0.0.1:8806",
  • basePath="/",
  • @OA\Info(
  • version="1.0.0",
  • title="interface document",
  • description="Version: 1.0.0",
  • @OA\Contact(name = "daydream", email = "heheiscool@163.com")
  • ),
    */

Notice the swagger format, where the line prefix starts with * and you replace it yourself (the editing here shows a little bit of a problem)
Then the following shows that the configuration is working:

4. The grammatical format of OpenApi 3.0 (swagger 3.0), it is recommended to refer to cases directly faster.Note here:

  • The original 2.0 @SWG was replaced by @OA
  • The types of interface parameter requests in are path, headers, cookies, no query, formData, etc.
  • formData is replaced by the keyword requestBody, which will be more flexible and functional

Case:

<?php

use OpenApi\Annotations as OA;

/**

  • @OA\Info(
  • version="1.0",
  • title="Example for response examples value"
  • )
    */

/**

  • @OA\Post(
  • path="/users",
  • summary="Adds a new user",
  • @OA\RequestBody(
  • @OA\MediaType(
  • mediaType="application/json",
  • @OA\Schema(
  • @OA\Property(
  • property="id",
  • type="string"
  • ),
  • @OA\Property(
  • property="name",
  • type="string"
  • ),
  • example={"id": 10, "name": "Jessica Smith"}
  • )
  • )
  • ),
  • @OA\Response(
  • response=200,
  • description="OK"
  • )
  • )
    */

As an example of a post request above, distinguish it from 2.0

<?php

namespace OpenApi\LinkExample;

/**

  • MVC controller that handles "users/" urls.
    /
    class UsersController
    {

    /**

    • @OA\Get(path="/2.0/users/{username}",
    • operationId="getUserByName",
    • @OA\Parameter(name="username",
    • in="path",
    • required=true,
    • @OA\Schema(type="string")
    • ),
    • @OA\Response(response="200",
    • description="The User",
    • @OA\JsonContent(ref="#/components/schemas/user"),
    • @OA\Link(link="userRepositories", ref="#/components/links/UserRepositories")
    • )
    • )
      */
      public function getUserByName($username)
      {
      }
      }

The above is an example of a get request
You can refer to the original document for details (I'll have time to complete each one later):
1,https://github.com/zircote/swagger-php
2,https://swagger.io/docs/specification/basic-structure/

Posted by Topper on Mon, 06 Apr 2020 09:27:14 -0700