Wednesday, March 22, 2017

Integrate Swagger API documentation with Laravel Project (L5 Swagger)



I assumed that you already installed Laravel in your localhost, for mine you can see below :

In this tutorial, we just use some commands to pull swagger and install it in Laravel project. Another post I will do the crud operation with swagger and also another post for security. So don't waste time lets some command to install it...

What is a Swagger?  Swagger is a swagger doc that allows us or developers to describe the structure of our APIs so that machines can read them.

According to the L5 Swagger in Github, you also can install it from their instruction too but it might hard because some command is not working properly. But I recommend all of you to check their site here because it might help you to understand more clearly. Let's do it:

1. Go to the terminal in your PHP Storm or cmd of your window like below:
please type this   composer require "darkaonline/l5-swagger:~4.0"


- CMD in window



- PHP Storm


after you type the command, you have to wait for a second and then we can continue another command... This command, it download some package like : doctrine/lexer,  doctrine/annotations, zircote/swagger-php, darkaonline/l5-swagger. You can all of them in vendor folder. And then you have to copy : \L5Swagger\L5SwaggerServiceProvider::class  
into folder config/app.php like below:



2.  Type php artisan l5-swagger:publish after you enter this command it will some folder from vendor to public folder



3. Type php artisan l5-swagger:publish-config to create a config file. From that file you config some information like: Title, auth_token, security_definition, key_inject....


4. Type php artisan l5-swagger:publish-assets to publish swagger-ui to your public folder

5. Type php artisan l5-swagger:publish-views to publish views (resources/views/vendor/l5-swagger)


6. Navigate to your localhost by adding this "/api/documentaion" like this: http://voyagers.dev:8000/api/documentation (I created virtual host) if not virtual host it should look like this: http://localhost:8000/mylaravel/public/api/documentation 
You will swagger is running like below but it is not yet working properly, so you have to generate api-docs.json:

7. First, you have to create a controller like php artisan make:controller ApiController  

and then you will see this file in controller folder and you have to add @SWG/Swagger above the controller like this: Don't forget to type this command to generate the api_docs.json :

php artisan l5-swagger:generate


but you don't have any data like GET, POST, DELETE, UPDATE because we do not yet create it.


8. We will create another DashboardController to show GET in Swagger. So please type this command: php artisan make:controller DashboardController DashboardController and add some data like below:


when you click on GET button (blue), you will response error because you not yet create a route to it so let create like this :

 Let's do a refresh and click you will response :


8 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. php artisan l5-swagger:publish-assets
    is not working.
    Could you please let me know that how can i run it ?

    ReplyDelete
  3. l5-swagger:publish-assets was removed since 5.0
    https://github.com/DarkaOnLine/L5-Swagger/issues/73

    ReplyDelete
  4. hello ! DevMarkete please visit bellow site and please give me an idea can i buy this gig from fiverr.com Laravel
    https://www.fiverr.com/ahmedwali5990/be-your-laravel-website-expert

    ReplyDelete
  5. Being Starter, I'm sure for life trying over the internet for the purpose of content pieces that might be from assistance to everybody. Regards. Leaking roof

    ReplyDelete
  6. Wow.. Very informative article thanks for sharing please keep it up because there is no limit of information so I'm waiting for an awesome article just like that.
    Crackjin
    IDM Crack
    TeamViewer Crack
    PhpStorm Crack
    Tenorshare iCareFone Crack

    ReplyDelete