JMSSerializer stand alone - Annotation does not exist, or cannot be auto-loaded

前端 未结 6 1513
遥遥无期
遥遥无期 2020-12-13 18:00

I am attempting to use JMSSerializer as a stand alone library to map JSON responses from an API to my model classes and am running into some issues.

Executing the fo

6条回答
  •  孤街浪徒
    2020-12-13 18:54

    @SirArturio has the correct answer to this Autoloading puzzle, and I just wanted to add a touch more clarity in response to @messified or anyone else struggling to get this working. As he eloquently explained, the automatic PSR-0 handler in composer, or SPL isn't going to cut it for loading these annotations since they use Doctrine's autoloading.

    So here is small complete example. Whenever you create your JMS Serializer object to begin serialization is a good time to add the annotation namespace to doctrine's autoloader. For clarity sake I'm assuming no IoC, and fully qualified namespaces (hint hint, use dependency injection):

    build();
    $json_output = $serializer->serialize('MyProject\MyClass', 'json');
    

    Then in your MyProject\MyClass:

    And that should cut it, autoloading the proper annotation file using doctrine instead of composer.

提交回复
热议问题