Sunday, November 27, 2016

Swagger implementation in laravel 5

Hi Everybody

Today we will learn how to integrate swagger into laravel 5.

Swagger creates the RESTful contract for our API, detailing all of its resources and operations in a readable format for easy development and integration.

Installation

For Swagger 2.0

 composer require "darkaonline/l5-swagger:~3.0"  

Open your AppServiceProvider (located in app/Providers) and add this line in register function

  $this->app->register(\L5Swagger\L5SwaggerServiceProvider::class);  

open your config/app.php and add this line in providers section

 \L5Swagger\L5SwaggerServiceProvider::class  

Configuration

 Run php artisan l5-swagger:publish to publish everything  
 Run php artisan l5-swagger:publish-config to publish configs (config/l5-swagger.php)  
 Run php artisan l5-swagger:publish-assets to publish swagger-ui to your public folder (public/vendor/l5-swagger)  
 Run php artisan l5-swagger:publish-views to publish views (resources/views/vendor/l5-swagger)  
 Run php artisan l5-swagger:generate to generate docs or set generate_always param to true in your config or .env file  

Now open your controller and write following script above class definition to define basic information about your api.

 /**  
  * @SWG\Swagger(  
  *   schemes={"http","https"},  
  *   host="dev.eduru.com",  
  *   basePath="/",  
  *   @SWG\Info(  
  *     version="1.0.0",  
  *     title="This is my website cool API",  
  *     description="Api description...",  
  *     termsOfService="",  
  *     @SWG\Contact(  
  *       email="contact@mysite.com"  
  *     ),  
  *     @SWG\License(  
  *       name="Private License",  
  *       url="URL to the license"  
  *     )  
  *   ),  
  *   @SWG\ExternalDocumentation(  
  *     description="Find out more about my website",  
  *     url="http..."  
  *   )  
  * )  
  */  
 class ProductController extends Controller  


Write following script above your function where you have defined your api functionality.

 /**  
    * @SWG\Get(  
    *   path="/api/v1/products",  
    *   description="Returns all pets from the system that the user has access to",  
    *   operationId="findPets",  
    *   produces={"application/json", "application/xml", "text/xml", "text/html"},  
    *   @SWG\Parameter(  
    *     name="tags",  
    *     in="query",  
    *     description="tags to filter by",  
    *     required=false,  
    *     type="array",  
    *     @SWG\Items(type="string"),  
    *     collectionFormat="csv"  
    *   ),  
    *   @SWG\Parameter(  
    *     name="limit",  
    *     in="query",  
    *     description="maximum number of results to return",  
    *     required=false,  
    *     type="integer",  
    *     format="int32"  
    *   ),  
    *   @SWG\Response(  
    *     response=200,  
    *     description="pet response",  
    *     @SWG\Schema(  
    *       type="array",  
    *       @SWG\Items(ref="#/definitions/pet")  
    *     ),  
    *   ),  
    *   @SWG\Response(  
    *     response="default",  
    *     description="unexpected error",  
    *     @SWG\Schema(  
    *       ref="#/definitions/errorModel"  
    *     )  
    *   )  
    * )  
    */  
 /**  
    * Display a listing of the resource.  
    * 
    */  
   public function getProducts()  
   {  
     //  
   }  


Now run follwoing command to publish.

 php artisan l5-swagger:publish  

Now open following link in your browser

http://yourdomain.com/api/documentation

In above link, we can see our api documentation and its implementation.

Thanks

2 comments:

  1. It's good for me to come back to your blog, it's been a few months for me. Well, this article which I have been waiting for so long, I need this article to complete my work in college, and it has only one topic with your article Thank you for sharing the best article post. Coronavirus Updates India COVID Tally Crosses 16 Lakh and the 55079 Positive Cases

    ReplyDelete