### NelmioApiDocBundle Installation Source: https://github.com/nelmio/nelmioapidocbundle/blob/5.x/docs/index.rst Instructions for installing the NelmioApiDocBundle using Composer. ```APIDOC ## Installation ### Description Install the NelmioApiDocBundle using Composer. ### Method ```bash composer require nelmio/api-doc-bundle ``` ### Configuration By default, only routes under ``/api`` are documented. Update the regexp at ``nelmio_api_doc.areas.path_patterns`` in ``config/packages/nelmio_api_doc.yaml`` to change this policy. If not using Flex, add the bundle to your kernel: ```php // AppKernel.php (or equivalent for your Symfony version) public function registerBundles(): iterable { $bundles = [ // ... new Nelmio\ApiDocBundle\NelmioApiDocBundle(), ]; // ... } ``` ### Registering Documentation Routes To browse your documentation with a UI, register one of the following routes in ``config/routes.yaml``: **Swagger UI:** ```yaml app.swagger_ui: path: /api/doc methods: GET defaults: { _controller: nelmio_api_doc.controller.swagger_ui } ``` **ReDoc:** ```yaml app.redocly: path: /api/doc methods: GET defaults: { _controller: nelmio_api_doc.controller.redocly } ``` **Stoplight:** ```yaml app.stoplight: path: /api/doc methods: GET defaults: { _controller: nelmio_api_doc.controller.stoplight } ``` To expose the documentation in JSON format: ```yaml app.swagger: path: /api/doc.json methods: GET defaults: { _controller: nelmio_api_doc.controller.swagger } ``` ### Filtering Routes To filter documented routes (e.g., exclude ``/_profiler/``), configure ``nelmio_api_doc.areas.path_patterns`` in ``config/packages/nelmio_api_doc.yaml``: ```yaml nelmio_api_doc: areas: path_patterns: - ^/api(?!/doc$) host_patterns: - ^api\. ``` ### Dependencies Twig and Assets packages are needed for the Swagger UI. ``` -------------------------------- ### Install Composer Dependencies for NelmioApiDocBundle Source: https://github.com/nelmio/nelmioapidocbundle/blob/5.x/README.md This command installs the Composer dependencies required to run the tests for the NelmioApiDocBundle. It is part of the setup process for contributing to or testing the bundle. ```bash composer update ``` -------------------------------- ### Install NelmioApiDocBundle using Composer Source: https://github.com/nelmio/nelmioapidocbundle/blob/5.x/README.md This command installs the latest version of the NelmioApiDocBundle using Composer, the dependency manager for PHP. Ensure you have Composer installed and in your system's PATH. ```bash composer require nelmio/api-doc-bundle ``` -------------------------------- ### Console command example in PHP Source: https://github.com/nelmio/nelmioapidocbundle/blob/5.x/UPGRADE-3.0.md Simple example of the api:doc:convert command to be run in the console to convert annotations. ```php ``` -------------------------------- ### Install NelmioApiDocBundle Assets (Bash) Source: https://github.com/nelmio/nelmioapidocbundle/blob/5.x/docs/faq.rst Provides the command to manually install NelmioApiDocBundle's static assets (CSS, JS, images) if they are not automatically installed via Composer scripts. This resolves 404 or 406 errors for these assets. ```bash $ php bin/console assets:install --symlink ``` -------------------------------- ### Example Default Configuration (YAML) Source: https://github.com/nelmio/nelmioapidocbundle/blob/5.x/docs/configuration_reference.rst A sample YAML configuration block demonstrating various settings for the NelmioApiDocBundle. ```yaml nelmio_api_doc: # Whether to use the symfony/type-info component for determining types. type_info: false # If true, `groups` passed to #[Model] attributes will be used to limit validation constraints use_validation_groups: false # Defines how to generate operation ids operation_id_generation: always_prepend cache: # define cache pool to use pool: null # define cache item id item_id: null # The documentation to use as base documentation: # Example: # info: # title: 'My App' # description: 'My App Description' # List of enabled Media Types media_types: - json # UI configuration options html_config: assets_mode: cdn # https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/ swagger_ui_config: [] # https://redocly.com/docs/redoc/config/ redocly_config: [] # https://docs.stoplight.io/docs/elements/b074dc47b2826-elements-configuration-options stoplight_config: [] # Filter the routes that are documented areas: default: path_patterns: # Examples: # - ^/api # - '^/api(?!/admin)' host_patterns: # Example: # - ^api name_patterns: # Example: # - ^api_v1 # whether to filter by attributes with_attribute: false security: # Authentication schemes https://swagger.io/docs/specification/v3_0/authentication/ MyBearerScheme: type: 'http' scheme: 'bearer' # if set disables default routes without attributes disable_default_routes: false # The base documentation used for the area documentation: # Example: # info: # title: 'My App' # description: 'My App Description' cache: # define cache pool to use for the area pool: null # define cache item id for the area item_id: null models: use_jms: false names: - alias: ~ # Example: 'Foo' type: ~ # Example: 'App\Foo' groups: null options: null serializationContext: [] areas: [] ``` -------------------------------- ### JSON Example for API Security Definitions Source: https://github.com/nelmio/nelmioapidocbundle/blob/5.x/docs/security.rst An example JSON snippet illustrating the structure for defining API security schemes and paths within an API documentation specification, likely OpenAPI/Swagger. ```json { "paths": { "/api/users": { "post": { "security": [ { "BearerAuthCustom": [ "bearer:read" ] } ] } } }, "components": { "securitySchemes": { "ApiKeyAuth": { "type": "apiKey", "name": "X-API-KEY", "in": "header" } } } } ``` -------------------------------- ### Configure Redocly route Source: https://github.com/nelmio/nelmioapidocbundle/blob/5.x/docs/index.rst This YAML configuration sets up a route for Redocly, another UI for viewing OpenAPI documentation. Accessing `/api/doc` via GET requests will render the Redocly interface. ```yaml # config/routes.yaml app.redocly: path: /api/doc methods: GET defaults: { _controller: nelmio_api_doc.controller.redocly } ``` -------------------------------- ### Run NelmioApiDocBundle Tests with PHPUnit Source: https://github.com/nelmio/nelmioapidocbundle/blob/5.x/README.md This command executes the test suite for the NelmioApiDocBundle using PHPUnit. This is typically run after installing dependencies to ensure the bundle is functioning correctly or to verify changes during development. ```bash composer phpunit ``` -------------------------------- ### Configure Stoplight route Source: https://github.com/nelmio/nelmioapidocbundle/blob/5.x/docs/index.rst This configuration defines a route for Stoplight's documentation viewer. By accessing `/api/doc` with a GET request, you can use Stoplight to explore your API. ```yaml # config/routes.yaml app.stoplight: path: /api/doc methods: GET defaults: { _controller: nelmio_api_doc.controller.stoplight } ``` -------------------------------- ### Fixing Asset Loading Issues Source: https://github.com/nelmio/nelmioapidocbundle/blob/5.x/docs/faq.rst Provides instructions on how to resolve 404 or 406 HTTP errors related to NelmioApiDocBundle's static assets (CSS, JS, images) by manually installing them. ```APIDOC ## Fixing Asset Loading Issues (404/406 Errors) ### Description This section addresses common issues where NelmioApiDocBundle's static assets (like CSS, JavaScript, and images) fail to load, resulting in 404 (Not Found) or 406 (Not Acceptable) HTTP status codes. This typically occurs when the assets are not automatically installed during the Composer process. ### Cause The assets are usually installed via a Composer script (`ScriptHandler::installAssets`) triggered by `post-install-cmd` or `post-update-cmd`. If this script is not configured or executed properly, the assets will not be placed in the web directory. ### Solution Manually trigger the asset installation command using the Symfony CLI: ### Command ```bash $ php bin/console assets:install --symlink ``` **Explanation:** * `php bin/console`: Executes the Symfony console commands. * `assets:install`: The command responsible for copying assets from vendor directories to the public web directory. * `--symlink`: Creates symbolic links instead of copying files, which is often useful during development for direct updates. ``` -------------------------------- ### Configure Area Security Scheme (YAML) Source: https://github.com/nelmio/nelmioapidocbundle/blob/5.x/docs/configuration_reference.rst Sets up a security scheme for a specific API documentation area. This example defines a bearer authentication scheme. ```yaml nelmio_api_doc: # ... areas: default: security: MyBearerScheme: type: 'http' scheme: 'bearer' ``` -------------------------------- ### Complete Symfony Controller Example with Attributes Source: https://github.com/nelmio/nelmioapidocbundle/blob/5.x/docs/symfony_attributes.rst Demonstrates a comprehensive example of using various NelmioApiDocBundle attributes within a Symfony controller for API documentation, including query string, query parameters, and request payloads. ```php-attributes namespace AppBundle\Controller; use AppBundle\UserDTO; use AppBundle\UserQuery; use OpenApi\Attributes as OA; use Symfony\Component\HttpKernel\Attribute\MapQueryParameter; use Symfony\Component\HttpKernel\Attribute\MapQueryString; use Symfony\Component\HttpKernel\Attribute\MapRequestPayload; use Symfony\Component\HttpKernel\Attribute\MapUploadedFile; use Symfony\Component\Routing\Annotation\Route; class UserController { /** * Find user with MapQueryString. */ #[Route('/api/users', methods: ['GET'])] #[OA\Parameter( name: 'userId', description: 'Id of the user to find', in: 'query', )] public function findUser(#[MapQueryString] UserQuery $userQuery) { // ... } /** * Find user with MapQueryParameter. */ #[Route('/api/users/v2', methods: ['GET'])] #[OA\Parameter( name: 'userId', description: 'Id of the user to find', in: 'query', )] ``` -------------------------------- ### Configure Operation ID Generation (YAML) Source: https://github.com/nelmio/nelmioapidocbundle/blob/5.x/docs/configuration_reference.rst Example of setting the operation ID generation strategy to 'always_prepend' in the YAML configuration file. ```yaml # config/packages/nelmio_api_doc.yaml nelmio_api_doc: operation_id_generation: always_prepend ``` -------------------------------- ### Configure Swagger UI Interface Endpoint Source: https://context7.com/nelmio/nelmioapidocbundle/llms.txt This snippet shows how to configure the route for the Swagger UI interface using `routes.yaml` and provides an example of NelmioApiDocBundle configuration in `nelmio_api_doc.yaml`. It allows access to interactive API documentation with support for multiple documentation areas and rendering options. ```yaml # In your config/routes.yaml: api_doc: path: /api/doc/{area} controller: Nelmio\ApiDocBundle\Controller\SwaggerUiController defaults: area: default methods: GET # Access the documentation: # Default area: GET /api/doc # Custom area: GET /api/doc/admin # Configure in config/packages/nelmio_api_doc.yaml: nelmio_api_doc: documentation: info: title: My API version: 1.0.0 areas: default: path_patterns: ['^/api'] admin: path_patterns: ['^/api/admin'] html_config: assets_mode: cdn swagger_ui_config: deepLinking: true displayRequestDuration: true ``` -------------------------------- ### Model Documentation with Validation Groups Source: https://github.com/nelmio/nelmioapidocbundle/blob/5.x/docs/index.rst This example demonstrates how to use validation groups with the #[Model] attribute when the 'use_validation_groups' configuration is enabled. It shows how constraints are applied based on specified groups. ```php-attributes use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Validator\Constraints as Assert; use OpenApi\Attributes as OA; class UserDto { #[Groups(['default', 'create', 'update'])] #[Assert\NotBlank(groups: ['default', 'create'])] public string $username; } // OpenAPI schema example using the model // shows `username` as `required` in the OpenAPI schema (not nullable) ``` -------------------------------- ### Configure JSON Documentation Export Endpoint Source: https://context7.com/nelmio/nelmioapidocbundle/llms.txt This configuration sets up a route in `routes.yaml` to export the OpenAPI specification in JSON format. This is useful for programmatic access, CI/CD integration, or consumption by other tools. The example demonstrates a basic JSON structure of the OpenAPI spec. ```yaml # In your config/routes.yaml: api_doc_json: path: /api/doc.json controller: Nelmio\ApiDocBundle\Controller\DocumentationController defaults: area: default methods: GET # Fetch the documentation: # curl http://localhost/api/doc.json # Response (JSON): { "openapi": "3.0.0", "info": { "title": "My API", "version": "1.0.0" }, "paths": { "/api/users": { "get": { "operationId": "get-api-users", "responses": { "200": { "description": "Success" } } } } } } ``` -------------------------------- ### Customize Swagger UI Font (Twig) Source: https://github.com/nelmio/nelmioapidocbundle/blob/5.x/docs/faq.rst Shows how to customize the font used in the NelmioApiDocBundle's Swagger UI by overriding the `index.html.twig` template. This example re-adds Google Fonts after they were removed for GDPR compliance. ```twig {# templates/bundles/NelmioApiDocBundle/SwaggerUi/index.html.twig #} {# To avoid a "reached nested level" error an exclamation mark `!` has to be added See https://symfony.com/blog/new-in-symfony-3-4-improved-the-overriding-of-templates #} {% extends '@!NelmioApiDoc/SwaggerUi/index.html.twig' %} {% block stylesheets %} {{ parent() }}