Install Swagger in Laravel 9

Install and Configure Laravel with Passport

First we install and configure laravel app with passport then next we install the open api l5 swagger for api documentations.

Step 1: Install Laravel App

First create a new Laravel application using the following mentioned command.

Step your database credentials in your env file.

Step 3: Install Laravel Passport

Install Laravel Passport using composer just run the below command.

Step 4: Migrate your Database

Run the following command to migrate your database.

Step 5: Install Passport

Next, to create the encryption keys needed to generate secured access tokens and save at secure place, run the command below

Step 6: Update User Model

After the installation process, add the Laravel\Passport\HasApiTokens trait to your App\User model as shown here

Step 7: Update Code in Service provider

Note: if you face error like: Call to undefined method Laravel\Passport\Passport::routes()
then simply skip this step

open the AuthServiceProvider file and update code just like below.

app/Providers/AuthServiceProvider.php

Step 8: Set the Driver Option

To authenticate any incoming API requests, open the config/auth configuration file and set the driver option of the API authentication guard to passport just like below code.

The laravel passport configuration is completed, Now we install and configure the Swagger for generating laravel documentations.

Integration of Swagger in Laravel Application

Now here step by step to install and configure the swagger api for laravel documentations.

Step 1: Install Swagger open Api

Now install the Swagger according to the Laravel version that you have installed. For more information visit DarkaOnLine/L5-Swagger

Step 2: Publish Swagger’s configuration

You can publish Swagger’s configuration and view files into your project by running the following command.

Step 3: Enable passport authentication

Enable passport authentication we need to uncomment Open API 3.0 support in security array of config/l5-swagger.php file

Step 4: Update Routes

Now, we need to create routes for API just update the below routes.

routes\api.php

Step 5: Create Controller

Now create a new controller AuthController using the following the command.

After genrating the controller update the below code on it.

app\Http\Controllers\Api\AuthController.php

Step 6: Generate Swagger

To generate the swagger documentation file just run php artisan l5-swagger: generate command.

More then times when you generate the swagger It will return an error

That means that you have to create that notation first. So let’s add it. I prefer creating Abstract controller for an API, but you can add this to app/Http/Controllers/Controller.php

Step 7: Open the Documentation

Note: we are using body parameter for creating api documentation in laravel with swagger open api. If you want to add query paramter then add some like below code.

Api Docuentation using Query Parameters

Below code for user register..

More examples of different @OA\Property types

  1. If You want to upload files (image, for zip, csv file) just update like below

2. You can use predefined format=” email” and even regexp pattern.

3. You can use type=” array” and collectionFormat=” multi” to describe an array of validation errors. You need to define @OA\Items annotation

4. When you need to describe an array of objects you can use type=” array” and pass object via @OA\Items

Code for when parameter in URL 

Whenever you need to describe parameter in URL (e.g. /api/users/{userId}/details )you can use @OA\Parameter.

That’s all the the Laravel API Documentation with Swagger Open Api and Passport is over now. There are more options and features of l5 swagger open api you can use.

Tags:  ,

Leave a reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">