2

How can I specify a metadata dir for the serializer used by FOSRestBundle, at controller level?

I can't set it up in config.yml because in my case it depends on the request's route.

I've seen in JMSSerializer doc that I could use the following code

$serializer = JMS\Serializer\SerializerBuilder::create()
    ->addMetadataDir($someDir)
    ->build();

But how to apply it to an already instanciated serializer (or how to replace it)?

marcv
  • 1,874
  • 4
  • 24
  • 45

1 Answers1

1

I'm afraid this isn't possible.

Directories are set to metadata drivers when calling build() in SerializerBuilder.php.

Even though you can access the metadata factory used by the Serializer it probably won't help you because the factory has nothing to do with cache directories. Only drivers work with directories.

So the only option for you is probably to create a new instance of Serializer and use that instead of the one from DI.

Edit: Creating a new Serializer works the same way as in your question. Then your DI container should be an instance of Container that has method set() which lets you override any registered service.

martin
  • 93,354
  • 25
  • 191
  • 226
  • Thanks for the answer. Could you add to your answer how I could *"create a new instance of Serializer and use that instead of the one from DI"* with FOSRestBundle? – marcv Sep 24 '16 at 19:15
  • OK, I still don't see how I could apply this to FOSRestBundle, but I'll accept your answer anyway. Thanks. – marcv Sep 26 '16 at 10:24