avkluchko/api-platform-extensions
Installation:
composer require avkluchko/api-platform-extensions
For non-Flex projects, enable the bundle in config/bundles.php:
return [
// ...
AVKluchko\ApiPlatformExtensions\ApiPlatformExtensionsBundle::class => ['all' => true],
];
First Use Case:
Enable AdminGroupsContextBuilder for automatic admin serialization groups:
# config/services.yaml
AVKluchko\ApiPlatformExtensions\Serializer\AdminGroupsContextBuilder:
decorates: 'api_platform.serializer.context_builder'
arguments: ['@AVKluchko\ApiPlatformExtensions\Serializer\AdminGroupsContextBuilder.inner']
Verify:
Test by accessing an API endpoint with an admin user. The response should include fields marked with @Groups({"admin:read"}).
Serialization Groups for Admins:
@Groups({"admin:read", "admin:write"}) in your entity properties.AdminGroupsContextBuilder automatically adds these groups when the user is authenticated as an admin.use AVKluchko\ApiPlatformExtensions\Serializer\AdminGroupsContextBuilder;
#[Groups(["default", "admin:read"])]
private $sensitiveData;
Event Subscribers:
KernelEvents::VIEW, ApiPlatform\Event\LifecycleEvent).use AVKluchko\ApiPlatformExtensions\EventSubscriber\ModifyResponseSubscriber;
class CustomResponseSubscriber extends ModifyResponseSubscriber {
public function onKernelView(KernelEvent $event) {
$response = $event->getResponse();
// Custom logic
}
}
services.yaml:
services:
App\EventSubscriber\CustomResponseSubscriber:
tags: ['kernel.event_subscriber']
Doctrine Extensions:
SoftDeletable or Timestampable traits for entities:
use AVKluchko\ApiPlatformExtensions\Doctrine\SoftDeletable;
use AVKluchko\ApiPlatformExtensions\Doctrine\Timestampable;
class User implements SoftDeletable, Timestampable {
// ...
}
Security Integration:
# config/packages/security.yaml
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
api-platform/core:^2.5.bundles.php.symfony/phpunit-bridge for testing event subscribers and serializers.Admin Groups Override:
AdminGroupsContextBuilder is misconfigured, admin-specific fields may not serialize. Verify the decorator setup in services.yaml.bin/console debug:container AVKluchko\ApiPlatformExtensions\Serializer\AdminGroupsContextBuilder
Event Subscriber Priority:
priority option in tags:
tags:
- { name: kernel.event_subscriber, priority: 100 }
Soft Deletes:
SoftDeletable entities are queried with isDeleted = false in repositories or DQL:
$query->andWhere('u.isDeleted = :deleted')
->setParameter('deleted', false);
Serialization Groups:
default, admin). Prefix custom groups (e.g., app:admin).Check Serialization Context:
bin/console debug:container api_platform.serializer.context_builder
Look for the decorated AdminGroupsContextBuilder.
Event Debugging: Enable Symfony's event dispatcher debug mode:
$dispatcher->addListener(KernelEvents::VIEW, function ($event) {
var_dump($event->getResponse()->getContent());
});
Custom Context Builders:
Extend AdminGroupsContextBuilder to add dynamic groups:
class CustomAdminGroupsContextBuilder extends AdminGroupsContextBuilder {
protected function getAdminGroups(): array {
return ['admin:read', 'admin:write', 'custom:group'];
}
}
Doctrine Lifecycle Callbacks:
Override prePersist, preUpdate, etc., in traits like Timestampable:
use AVKluchko\ApiPlatformExtensions\Doctrine\Timestampable;
class Post implements Timestampable {
protected $createdAt;
protected $updatedAt;
public function prePersist() {
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
}
}
API Resource Extensions:
Use ApiResource extensions for custom metadata:
use AVKluchko\ApiPlatformExtensions\ApiResource\ExtensionInterface;
class CustomExtension implements ExtensionInterface {
public function getOperations() {
return [
'get' => ['method' => 'GET', 'path' => '/custom-path'],
];
}
}
symfony/config and symfony/dependency-injection versions match (^5.1.5).^2.5. For newer versions, check for compatibility or fork the package.How can I help you explore Laravel packages today?