Login to bookmark this video
Buy Access to Course
29.

Custom Normalizer

|

Share this awesome video!

|

Keep on Learning!

5 Comments

Sort By
Login or Register to join the conversation
Alex-K avatar Alex-K 1 year ago

#[AsDecorator('api_platform.jsonld.normalizer.item')]
class AddOwnerGroupsNormalizer implements NormalizerInterface

What if I want to keep support for other formats as well? Like JSON-HAL etc...
Should I remove AsDecorator from my normalizer and move the decoration config to services.yaml as mentioned towards the end of the article?

| Reply |

Hey @Alex-K!

Sorry for the slow reply! That's a great question :).

Should I remove AsDecorator from my normalizer and move the decoration config to services.yaml as mentioned towards the end of the article?

I believe so, yes. As far as the last time I checked, a single service can't decorate multiple services. So if you're decorating 2 services, you need to have, your self, 2 services, where each decorates one service. In services.yaml that's where you can set up multiple services for this one class (and then have each service decorate one core service).

Cheers!

1 | Reply |
Cyril avatar Cyril 1 year ago

Hi! I have an issue by using a custom normalizer (but not with API Platform). As mentioned in the Symfony documentation, it's possible to create a custom normalizer and inject services in its constructor :

class MyCustomNormalizer implements NormalizerInterface
{
    public function __construct(
        private UrlGeneratorInterface $router,
        private ObjectNormalizer $normalizer,
    ) {
    }

But when I try to instantiate it by calling $serializer = new Serializer([new MyCustomNormalizer()], [new Converter()]) I have an issue like "to few arguments for MyCustomNormalizer: 0 passed, 2 expected..."

What is the correct way to do that?
Thanks for your help
Cyril

| Reply |

Hi,

I'm not fully understand what are you trying to do, but in Symfony(not ApiPlatform) all you need just create this normalizer and that's all, it will be automatically registered in system and it will be used for objects which are defined in support() method

$serializer = new Serializer([new MyCustomNormalizer()], [new Converter()])

Such things will never work, you are instantiating Classes manually so you need to get all arguments from service container or instantiate them as they are defined!

Cheers!

| Reply |

Thanks for your help, I didn't realize custom normalizers don't need to be called manually!

| Reply |

Delete comment?

Share this comment

astronaut with balloons in space

"Houston: no signs of life"
Start the conversation!