How to install swagger on symfony 4?

by raven_corwin , in category: PHP Frameworks , 9 months ago

How to install swagger on symfony 4?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by orpha , 9 months ago

@raven_corwin 

To install Swagger on Symfony 4, you can follow these steps:

  1. Install the NelmioApiDocBundle using composer:
1
composer require nelmio/api-doc-bundle


  1. Enable the bundle in config/bundles.php:
1
NelmioApiDocBundleNelmioApiDocBundle::class => ['all' => true]


  1. Configure the bundle in your config/packages/nelmio_api_doc.yaml file:
1
2
3
nelmio_api_doc:
    areas:
        path_patterns: ['/api/*']


  1. Add annotations to your controller methods to provide Swagger documentation. You can use annotations like @SWGGet, @SWGResponse, @SWGParameter, etc. For example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
/**
 * @SWGGet(
 *     path="/api/users",
 *     tags={"Users"},
 *     summary="Get all users",
 *     @SWGResponse(response="200", description="Success"),
 *     @SWGResponse(response="404", description="Not found")
 * )
 */
public function getUsers()
{
    // your logic here
}


  1. Access the Swagger documentation by visiting /api/doc in your browser. This will display the Swagger UI with the documentation for your API endpoints.


That's it! You should now have Swagger documentation set up for your Symfony 4 application.