Installation Add the package via Composer:
composer require api-platform/documentation
Register the bundle in config/app.php under providers:
ApiPlatform\Doctrine\Orm\Extension\DocumentationExtension::class,
First Use Case
Enable OpenAPI/Swagger documentation in config/packages/api_platform.yaml:
api_platform:
formats:
jsonld: ['application/ld+json']
json: ['application/json']
html: ['text/html']
documentation:
enabled: true
api_keys:
- { name: 'Authorization', type: 'header' }
Access docs at /api/doc (or /api.jsonld for machine-readable format).
Where to Look First
/api/doc (interactive UI).src/Extension/DocumentationExtension.php (core logic).config/packages/api_platform.yaml (customization).Generating Documentation
@ApiResource).@ApiProperty/@ApiFilter annotations to customize schema.
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiFilter;
#[ApiResource]
class Book {
#[ApiProperty(description: "The book's title")]
public string $title;
#[ApiFilter(SearchFilter::class, properties: ["title"])]
public string $author;
}
Integration with Frontend
/api.jsonld to frontend apps (e.g., React/Vue) for dynamic API exploration.<iframe src="/api/doc" width="100%" height="600px"></iframe>
Customizing Output
assets/swagger-ui/ or override templates in templates/bundles/apiplatform/documentation/.config/packages/api_platform.yaml:
documentation:
info:
title: "My Awesome API"
description: "Built with Laravel & API Platform"
Versioning
api_platform.version middleware to version endpoints (e.g., /api/v1/docs).@ApiProperty if not needed.ApiPlatform\EventListener\Event to modify docs dynamically (e.g., add auth schemes).php bin/console api:doc to generate docs without a full server.Caching Issues
php bin/console cache:clear
config/packages/api_platform.yaml during development:
documentation:
enabled: true
cache: false
Annotation Conflicts
ApiPlatform\Core\Annotation (not ApiPlatform\Core\Bridge\Doctrine).#[\ApiResource]) for future compatibility.Missing Endpoints
/api/doc returns 404, ensure:
DocumentationExtension is registered.doc route in routes/api.php).Complex Relationships
@ApiResource with collectionOperations/itemOperations explicitly./api.jsonld to inspect raw OpenAPI spec.api_platform:
documentation:
debug: true
php bin/console debug:container ApiPlatform\Metadata\Resource\Factory\ResourceMetadataFactory to verify resource metadata.Custom Schemes
Add OAuth2 or JWT support via documentation.api_keys:
documentation:
api_keys:
- { name: "Authorization", type: "header", scheme: "bearer" }
Post-Processing
Extend ApiPlatform\Metadata\Resource\Factory\ResourceMetadataFactoryDecorator to modify docs before generation.
Theming Override Swagger UI assets by publishing templates:
php bin/console assets:install public
Then customize public/swagger-ui/index.html.
Server-Side Rendering
For Laravel Blade integration, use the api_platform.documentation service to generate HTML:
$docs = $this->container->get('api_platform.documentation');
echo $docs->getHtml();
How can I help you explore Laravel packages today?