Installation Add the package via Composer:
composer require api-platform/hydra
Ensure api-platform/core is also installed (Hydra is a subcomponent).
First Use Case: Hydra Documentation Generation
Hydra generates interactive API documentation (like Swagger/OpenAPI) dynamically. Start by configuring it in config/packages/api_platform.yaml:
api_platform:
formats:
jsonld: ['application/ld+json']
hydra:
enabled: true
output_dir: '%kernel.project_dir%/public/docs'
Run the generator:
php bin/console api:hydra:generate
Access docs at /docs/index.html.
Where to Look First
ApiPlatform\Metadata\Resource\ResourceMetadataFactory (for resource metadata).ApiPlatform\Hydra\Generator\HydraGenerator (for customization).config/packages/api_platform.yaml (hydra section).Dynamic Documentation Hydra auto-generates OpenAPI/Swagger docs from your API resources. Extend it by:
@ApiProperty in entities).// src/Hydra/CustomGenerator.php
use ApiPlatform\Hydra\Generator\HydraGenerator;
class CustomHydraGenerator extends HydraGenerator {
public function generate(): void {
parent::generate();
// Add custom logic (e.g., inject examples)
}
}
Register it in services.yaml:
services:
App\Hydra\CustomGenerator:
decorates: 'api_platform.hydra.generator'
arguments: ['@.inner']
Integration with API Platform
Hydra works seamlessly with API Platform’s Resource and Operation classes. Use it to:
Partial Generation Generate docs for specific resources only:
php bin/console api:hydra:generate --resource=App\Entity\Post
# config/packages/api_platform.yaml
hydra:
cache: true
vendor/api-platform/hydra/templates) for branding or additional UI elements.Metadata Conflicts
@ApiResource and related annotations correctly. Missing or conflicting metadata (e.g., duplicate @ApiProperty) can break doc generation.php bin/console debug:api
Circular References
ManyToMany) may cause infinite loops in schema generation.@ApiProperty(ignoreNull: true) or @MaxDepth to limit traversal depth.Output Directory Permissions
public/docs by default. Ensure the directory is writable:
chmod -R 755 public/docs
Deprecated Features
-v for detailed logs:
php bin/console api:hydra:generate -v
public/docs.Custom Schemas
Extend Hydra’s schema generation by implementing ApiPlatform\Hydra\Generator\SchemaGeneratorInterface:
use ApiPlatform\Hydra\Generator\SchemaGeneratorInterface;
use ApiPlatform\Metadata\Resource\ResourceMetadata;
class CustomSchemaGenerator implements SchemaGeneratorInterface {
public function generateSchema(ResourceMetadata $resource): array {
$schema = parent::generateSchema($resource);
// Modify $schema as needed
return $schema;
}
}
Register it in services.yaml:
services:
App\Hydra\CustomSchemaGenerator:
tags: ['api_platform.hydra.schema_generator']
Post-Processing
Hook into Hydra’s output by extending the generator’s postGenerate method or using Symfony’s event system:
# config/services.yaml
services:
App\EventListener\HydraPostGenerateListener:
tags:
- { name: kernel.event_listener, event: api_platform.hydra.post_generate, method: onPostGenerate }
Non-Standard Formats
Hydra supports JSON-LD by default. For custom formats (e.g., YAML), override the generate method in your custom generator and write to a different file extension.
APP_URL env var is set correctly.hydra.enabled in api_platform.yaml and clear the cache:
php bin/console cache:clear
How can I help you explore Laravel packages today?