How to integrate Swagger in Lumen/Laravel for REST API?

前端 未结 2 1979
無奈伤痛
無奈伤痛 2021-02-03 11:40

I have built some REST API in Lumen micro framework and it\'s working fine. Now I want to integrate Swagger into it so the API will be well documented on future use. Has anyone

2条回答
  •  失恋的感觉
    2021-02-03 11:51

    Steps to follow for Laravel Lumen 5.7 with swagger using OpenApi 3.0 specs (this governs the way you write annotations so that swagger documentation is generated)

    I reached this adjusting on @black-mamba answer in order to make it work.

    1. Install swagger-lume dependency (which also installs swagger)

    composer require "darkaonline/swagger-lume:5.6.*"
    

    2. Edit bootstrap/app.php file

    make sure $app->withFacades(); is NOT commented (around line 26)

    in Create The Application section add the following before Register Container Bindings section

    $app->configure('swagger-lume');
    

    in Register Service Providers section add

    $app->register(\SwaggerLume\ServiceProvider::class);
    

    3. Publish configuration file for swagger-lume

    php artisan swagger-lume:publish
    

    4. Add annotations to your code

    For example in your app/Http/Controllers/Controller.php you could have the general part of the documentation

    And inside each of your controllers you should have the appropriate annotations above each public method

    input("criteria");
            if (! isset($category)) {
                return response()->json(null, Response::HTTP_BAD_REQUEST);
            }
    
            // ...
    
            return response()->json(["thing1", "thing2"], Response::HTTP_OK);
        }
    }
    

    5. Generate swagger documentation

    php artisan swagger-lume:generate
    

    Run this each time you update the documentation


    see:

    • https://zircote.github.io/swagger-php/
    • https://github.com/DarkaOnLine/SwaggerLume
    • https://swagger.io/specification/

提交回复
热议问题