This bundle provides integration of league/fractal for Symfony. In addition it allows you to use transformers as a services.
This is a fork version of samjarrett/FractalBundle.
Requirements:
- PHP >= 7.4
- Symfony 4, 5 and 6
Install through composer:
composer require fd6130/fractal-bundle
If you are using symfony flex, it will automatic register the bundle for you.
You can use command php bin/console make:fractal-transformer
to create a transformer.
Or, just create it by your own and place it in src/Transformer
.
class UserTransformer extends TransformerAbstract
{
public function transform(User $user): array
{
$data = [
'id' => $user->id(),
'name' => $user->name(),
];
return $data;
}
}
$resource = new Collection($users, UserTransformer::class);
$response = $manager->createData($resource)->toArray();
You can inject services to your transformer through constructor:
class UserTransformer extends TransformerAbstract
{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public function transform(User $user): array
{
$data = [
'id' => $user->id(),
'name' => $user->name(),
];
// $this->entityManager->getRepository(...)
return $data;
}
}