安装swagger包

  • 链接:swagger包
  • 安装:composer require zircote/swagger-php 版本:”zircote/swagger-php”: “^4.4”

    构建Controller和route

    ```php <?php

    namespace App\Http\Controllers\NoMiddle;

use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Swagger\Annotations\Info; use Swagger\Annotations\OA;

/**

  • @OA\Info(title=”属于我自己的接口”, version=”0.1”, description=”债务模块的接口以及自己测试的接口”) */ class SwaggerController extends Controller { public function doc() { $swagger = \OpenApi\Generator::scan([
    1. app_path('Http/Controllers/NoMiddle'), // 第一个地址里面要有@OA\Info
    2. app_path('Http/Controllers/DebtSelf'), // 其他的控制器目录地址
    ]); return response()->json($swagger); } }

// swagger 文档 Route::get(‘/swagger/doc’, ‘SwaggerController@doc’);

  1. <a name="ENReT"></a>
  2. # 安装swagger-ui
  3. - 在public下执行`git clone [https://github.com/swagger-api/swagger-ui](https://github.com/swagger-api/swagger-ui)`会将前端代码添加进swagger-ui目录
  4. - git clone拷贝完之后呢,其实我们只需要 dist目录就行,其他的目录以及文件可以删掉
  5. - 将其中的dist中的index.html改为
  6. ```html
  7. <!-- HTML for static distribution bundle build -->
  8. <!DOCTYPE html>
  9. <html lang="en">
  10. <head>
  11. <meta charset="UTF-8">
  12. <title>Swagger UI</title>
  13. <link rel="stylesheet" type="text/css" href="./swagger-ui.css" />
  14. <link rel="stylesheet" type="text/css" href="index.css" />
  15. <link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
  16. <link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
  17. </head>
  18. <body>
  19. <div id="swagger-ui"></div>
  20. <script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
  21. <script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
  22. <script src="./swagger-initializer.js" charset="UTF-8"> </script>
  23. <script>
  24. window.onload = function() {
  25. // Begin Swagger UI call region
  26. const ui = SwaggerUIBundle({
  27. // url: "https://petstore.swagger.io/v2/swagger.json",
  28. url: "/swagger/doc", //新的配置
  29. dom_id: '#swagger-ui',
  30. deepLinking: true,
  31. presets: [
  32. SwaggerUIBundle.presets.apis,
  33. SwaggerUIStandalonePreset
  34. ],
  35. plugins: [
  36. SwaggerUIBundle.plugins.DownloadUrl
  37. ],
  38. layout: "StandaloneLayout"
  39. })
  40. // End Swagger UI call region
  41. window.ui = ui
  42. }
  43. </script>
  44. </body>
  45. </html>
  • 就可以直接通过/swagger-ui/dist/index.html路径来访问到swagger文档了

创建新的控制器以及接口

  1. <?php
  2. namespace App\Http\Controllers\DebtSelf;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use App\Services\ValidaParamService;
  6. /**
  7. * @OA\Tag(
  8. * name="债务",
  9. * description="何日可以出坑",
  10. * )
  11. */
  12. class DebtIndexController extends Controller
  13. {
  14. private $verifyParamService;
  15. function __construct(
  16. ValidaParamService $verifyParamService
  17. ){
  18. $this->verifyParamService = $verifyParamService;
  19. }
  20. /**
  21. * @OA\Post(
  22. * path="/debtSelf/add",
  23. * tags={"债务"},
  24. * summary="添加",
  25. * @OA\Parameter(name="isFastLogin", in="query", @OA\Schema(type="boolean")),
  26. * @OA\Parameter(name="tel", in="query", @OA\Schema(type="string")),
  27. * @OA\Parameter(name="verification_code", in="query", @OA\Schema(type="string")),
  28. * @OA\Parameter(name="password", in="query", @OA\Schema(type="string")),
  29. * @OA\Response(response=200, description=" {err_code: int32, msg:string, data:[]}")
  30. * )
  31. */
  32. public function add(Request $request)
  33. {
  34. try {
  35. $data = [ "code" => 200, "data" => app_path('Http/Controllers/DebtSelf') ];
  36. $rule = [];
  37. $param = $this->verifyParamService
  38. ->init($rule, $request->all())
  39. ->filter()
  40. ->needRequired()
  41. ->param;
  42. return response()->json($data);
  43. } catch(\Exception $e) {
  44. return errorLog($e);
  45. }
  46. }
  47. }

关于注解详细请看:https://github.com/zircote/swagger-php/blob/master/docs/Migrating-to-v3.md