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

前端 未结 2 1980
無奈伤痛
無奈伤痛 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:44

    I had really hard time in learning how to use it. Here I'm going to share some of the things I did to get it working

    Use this wrapper

    Run this command in your terminal:

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

    Or older version from the repo linked if this doesn't work for you

    Then from the repo follow these steps:

    Open your bootstrap/app.php file and: uncomment this line (around line 26) in Create The Application section:

     $app->withFacades(); add this line before Register Container Bindings section:
    
     $app->configure('swagger-lume'); add this line in Register Service Providers section:
    
    $app->register(\SwaggerLume\ServiceProvider::class);
    

    Then you'll need to use annotations for your project instead of YAML or JSON For more Create a Annotation.php file in your app folder add the following a sample

    /**
     * @SWG\Swagger(
     *     basePath="/",
     *     schemes={"http"},
     *     @SWG\Info(
     *         version="1.0.0",
     *         title="HAVE MY BOOKS",
     *         @SWG\Contact(
     *             email="example@test.com"
     *         ),
     *     )
     * )
     */
    /**
    * @SWG\Get(
     *   path="/",
     *   summary="Version",
     *   @SWG\Response(
     *     response=200,
     *     description="Working"
     *   ),
     *   @SWG\Response(
     *     response="default",
     *     description="an ""unexpected"" error"
     *   )
     * )
     */
    

    Then run

    php artisan swagger-lume:publish
    

    Then run

    php artisan swagger-lume:generate
    

    This generates JSON which can be accessed from your localhost:8000 or whichever port you are serving your LUMEN service

    Note: after creating an issue in the repo I found that to access you'll need to serve or run using

    php -S localhost:8000 public/index.php
    

    Because of some PHP routing issue with php -S localhost:8000 public

提交回复
热议问题