Convert Figma logo to code with AI

nelmio logoNelmioApiDocBundle

Generates documentation for your REST API from annotations

2,218
833
2,218
147

Top Related Projects

This Bundle provides various tools to rapidly develop RESTful API's with Symfony

🕸️ Create REST and GraphQL APIs, scaffold Jamstack webapps, stream changes in real-time.

A php swagger annotation and parsing library

36,008

Beautiful static documentation for your API

Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.

The OpenAPI Specification Repository

Quick Overview

NelmioApiDocBundle is a Symfony bundle that generates documentation for your API automatically. It extracts information from your routes, controllers, and models to create comprehensive API documentation, which can be presented in various formats including Swagger UI.

Pros

  • Automatically generates API documentation from existing code
  • Supports multiple output formats, including Swagger/OpenAPI
  • Integrates seamlessly with Symfony framework
  • Customizable through annotations and configuration

Cons

  • May require additional configuration for complex API structures
  • Documentation quality depends on how well the code is annotated
  • Limited support for non-Symfony PHP projects
  • Learning curve for advanced customization

Code Examples

  1. Basic route annotation:
use OpenApi\Annotations as OA;

/**
 * @OA\Get(
 *     path="/api/users",
 *     @OA\Response(response="200", description="List users")
 * )
 */
public function listUsers()
{
    // ...
}
  1. Defining request body:
use OpenApi\Annotations as OA;

/**
 * @OA\Post(
 *     path="/api/users",
 *     @OA\RequestBody(
 *         @OA\JsonContent(
 *             type="object",
 *             @OA\Property(property="name", type="string"),
 *             @OA\Property(property="email", type="string")
 *         )
 *     ),
 *     @OA\Response(response="201", description="User created")
 * )
 */
public function createUser()
{
    // ...
}
  1. Configuring API information:
nelmio_api_doc:
    documentation:
        info:
            title: My API
            description: This is a sample API
            version: 1.0.0
    areas:
        path_patterns:
            - ^/api(?!/doc$)

Getting Started

  1. Install the bundle:

    composer require nelmio/api-doc-bundle
    
  2. Enable the bundle in config/bundles.php:

    return [
        // ...
        Nelmio\ApiDocBundle\NelmioApiDocBundle::class => ['all' => true],
    ];
    
  3. Configure the bundle in config/packages/nelmio_api_doc.yaml:

    nelmio_api_doc:
        documentation:
            info:
                title: My API
                description: This is my API documentation
                version: 1.0.0
    
  4. Add the route for the documentation UI in config/routes.yaml:

    app.swagger_ui:
        path: /api/doc
        methods: GET
        defaults: { _controller: nelmio_api_doc.controller.swagger_ui }
    

Now you can start annotating your controllers and access the documentation at /api/doc.

Competitor Comparisons

This Bundle provides various tools to rapidly develop RESTful API's with Symfony

Pros of FOSRestBundle

  • Provides a comprehensive set of tools for building RESTful APIs in Symfony
  • Offers flexible configuration options for request/response handling and routing
  • Includes built-in support for content negotiation and view layer

Cons of FOSRestBundle

  • Steeper learning curve due to its extensive feature set
  • May introduce unnecessary complexity for simpler API projects
  • Less focus on API documentation compared to NelmioApiDocBundle

Code Comparison

FOSRestBundle:

use FOS\RestBundle\Controller\Annotations as Rest;

/**
 * @Rest\View()
 */
public function getUserAction($id)
{
    // Controller logic
}

NelmioApiDocBundle:

use Nelmio\ApiDocBundle\Annotation\Model;
use OpenApi\Annotations as OA;

/**
 * @OA\Get(
 *     path="/api/users/{id}",
 *     @OA\Response(response="200", description="User details")
 * )
 */
public function getUserAction($id)
{
    // Controller logic
}

FOSRestBundle focuses on RESTful API implementation, while NelmioApiDocBundle emphasizes API documentation. FOSRestBundle provides annotations for view handling and routing, whereas NelmioApiDocBundle uses OpenAPI annotations for generating API documentation. Both bundles can be used together in a Symfony project to create well-documented and RESTful APIs.

🕸️ Create REST and GraphQL APIs, scaffold Jamstack webapps, stream changes in real-time.

Pros of API Platform

  • Full-featured API framework with built-in support for GraphQL, JSON-LD, and Hydra
  • Automatic CRUD operations and advanced filtering capabilities
  • Seamless integration with Symfony and Doctrine ORM

Cons of API Platform

  • Steeper learning curve due to its comprehensive feature set
  • May be overkill for simple API documentation needs
  • Less flexibility in customizing documentation output

Code Comparison

NelmioApiDocBundle:

/**
 * @ApiDoc(
 *     description="Get a user by ID",
 *     parameters={
 *         {"name"="id", "dataType"="integer", "required"=true, "description"="User ID"}
 *     }
 * )
 */
public function getUserAction($id) { /* ... */ }

API Platform:

#[ApiResource]
#[Get(description: "Get a user by ID")]
class User
{
    #[ApiProperty(identifier: true)]
    public int $id;
    // ...
}

API Platform offers a more declarative approach, leveraging PHP attributes to define API resources and operations. NelmioApiDocBundle relies on PHPDoc annotations for documentation, which may be more familiar to some developers but less powerful for complex APIs.

While NelmioApiDocBundle focuses primarily on API documentation, API Platform provides a complete framework for building and documenting APIs, including features like automatic CRUD operations and advanced filtering. However, this comprehensive approach may be excessive for projects that only require simple documentation.

A php swagger annotation and parsing library

Pros of swagger-php

  • Framework-agnostic, can be used with any PHP project
  • Generates OpenAPI/Swagger documentation directly from PHP annotations
  • Supports the latest OpenAPI 3.0 specification

Cons of swagger-php

  • Requires manual annotation of code, which can be time-consuming
  • Less integrated with Symfony ecosystem compared to NelmioApiDocBundle
  • May require additional setup for generating and serving documentation

Code Comparison

NelmioApiDocBundle:

/**
 * @Route("/api/users", name="get_users", methods={"GET"})
 * @ApiDoc(
 *     section="Users",
 *     description="Get all users",
 *     output={"class"=User::class, "collection"=true}
 * )
 */
public function getUsers() { /* ... */ }

swagger-php:

/**
 * @OA\Get(
 *     path="/api/users",
 *     summary="Get all users",
 *     @OA\Response(response="200", description="Successful operation")
 * )
 */
public function getUsers() { /* ... */ }

Both libraries offer ways to generate API documentation, but swagger-php provides more flexibility across different PHP frameworks at the cost of less integration with Symfony-specific features.

36,008

Beautiful static documentation for your API

Pros of Slate

  • Language-agnostic: Works with any API, not tied to a specific framework
  • Highly customizable: Offers more control over the look and feel of documentation
  • Single-page design: Provides a smooth, scrollable interface for easy navigation

Cons of Slate

  • Requires more manual effort: Documentation must be written in Markdown
  • Separate from codebase: Not directly integrated with the API code
  • Steeper learning curve: May require more time to set up and customize

Code Comparison

NelmioApiDocBundle (PHP):

/**
 * @OA\Get(
 *     path="/api/users",
 *     @OA\Response(response="200", description="List users")
 * )
 */
public function listUsers()
{
    // ...
}

Slate (Markdown):

## List Users

GET /api/users

> Response:

```json
[
  {
    "id": 1,
    "name": "John Doe"
  }
]

NelmioApiDocBundle is tightly integrated with Symfony and uses PHP annotations for documentation, while Slate relies on manually written Markdown files. NelmioApiDocBundle automatically generates documentation from code, whereas Slate offers more flexibility in presentation but requires separate maintenance of documentation.

Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.

Pros of Swagger UI

  • Platform-agnostic and can be used with any API framework or language
  • Offers a standalone, interactive UI for API documentation
  • Widely adopted and supported by a large community

Cons of Swagger UI

  • Requires manual creation and maintenance of OpenAPI/Swagger specification files
  • Less integrated with Symfony framework compared to NelmioApiDocBundle
  • May require additional setup and configuration for complex APIs

Code Comparison

NelmioApiDocBundle (Symfony-specific):

/**
 * @Route("/api/users", name="api_users_list")
 * @Method("GET")
 * @ApiDoc(
 *     resource=true,
 *     description="List all users"
 * )
 */
public function listUsersAction()
{
    // ...
}

Swagger UI (OpenAPI specification):

paths:
  /api/users:
    get:
      summary: List all users
      description: Retrieves a list of all users
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/User'

NelmioApiDocBundle is tightly integrated with Symfony and allows for inline documentation within PHP code. Swagger UI relies on separate OpenAPI specification files, offering more flexibility but requiring manual maintenance.

The OpenAPI Specification Repository

Pros of OpenAPI-Specification

  • Language-agnostic, allowing for broader adoption across different tech stacks
  • More comprehensive and standardized approach to API documentation
  • Wider ecosystem of tools and integrations due to its status as an industry standard

Cons of OpenAPI-Specification

  • Steeper learning curve for newcomers compared to NelmioApiDocBundle
  • Requires more manual effort to create and maintain documentation
  • Less tightly integrated with Symfony framework

Code Comparison

NelmioApiDocBundle:

/**
 * @Route("/api/users", name="get_users", methods={"GET"})
 * @ApiDoc(
 *     section="Users",
 *     description="Get all users",
 *     output={"class"=User::class, "collection"=true}
 * )
 */
public function getUsers() { /* ... */ }

OpenAPI-Specification:

/api/users:
  get:
    summary: Get all users
    description: Retrieve a list of all users
    responses:
      '200':
        description: Successful response
        content:
          application/json:
            schema:
              type: array
              items:
                $ref: '#/components/schemas/User'

While NelmioApiDocBundle offers a more PHP-centric approach with annotations, OpenAPI-Specification provides a more detailed and language-agnostic YAML-based definition. OpenAPI-Specification allows for more granular control over API documentation but requires more manual effort, whereas NelmioApiDocBundle integrates more seamlessly with Symfony projects at the cost of less flexibility.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

NelmioApiDocBundle

Build Status Total Downloads Latest Stable
Version

The NelmioApiDocBundle bundle allows you to generate a decent documentation for your APIs.

Migrate from 3.x to 4.0

To migrate from 3.x to 4.0, follow our guide.

Version 4.0 brings OpenAPI 3.0 support. If you want to stick to Swagger 2.0, you should use the version 3 of this bundle.

Migrate from 2.x to 3.0

To migrate from 2.x to 3.0, follow our guide.

Installation

Open a command console, enter your project directory and execute the following command to download the latest version of this bundle:

composer require nelmio/api-doc-bundle

Documentation

Read the documentation on symfony.com

Contributing

See CONTRIBUTING file.

Running the Tests

Install the Composer dependencies:

git clone https://github.com/nelmio/NelmioApiDocBundle.git
cd NelmioApiDocBundle
composer update

Then run the test suite:

./phpunit

License

This bundle is released under the MIT license.