Bundle to define automatically all groups on all entities for ApiPlatform.
Install ApiPlatformAutoGroupBundle library using Composer:
composer require bigchicchicken/api-platform-auto-group-bundle
Add/Check activation in the file config/bundles.php
:
// config/bundles.php
return [
// ...
ApiPlatformAutoGroupBundle\ApiPlatformAutoGroupBundle::class => ['all' => true],
];
By default, the following strategy is applied to all entities:
Normalization groups:
{ShortName}:read
{ShortName}:read:{OperationName}
Denormalization groups:
{ShortName}:write
{ShortName}:write:{OperationName}
(Default operation name: GetCollection
Post
Get
Put
Delete
Patch
)
But if you want to use your custom strategy, just override the service like that:
- Create a file that extend from
AutoGroupResourceMetadataCollectionFactory
.
<?php
// src/ResourceMetadataCollectionFactory/CustomResourceMetadataCollectionFactory.php
namespace App\ResourceMetadataCollectionFactory;
use ApiPlatformAutoGroupBundle\ResourceMetadataCollectionFactory\AutoGroupResourceMetadataCollectionFactory;
class CustomResourceMetadataCollectionFactory extends AutoGroupResourceMetadataCollectionFactory
{
/**
* {@inheritDoc}
*/
protected function getDefaultGroups(string $shortName, bool $normalization, string $operationName): array
{
return [
'my_custom_strategy'
];
}
}
- And override the service
api_platform.auto_group.resource.metadata_collection_factory
with the previous class.
# config/services.yaml
services:
api_platform.auto_group.resource.metadata_collection_factory:
class: App\ResourceMetadataCollectionFactory\CustomResourceMetadataCollectionFactory
decorates: api_platform.metadata.resource.metadata_collection_factory
arguments:
- '@.inner'
This is completely free and released under the MIT License.