JMSSerializerBundle mixing auto detection and explicit directories?

时光总嘲笑我的痴心妄想 提交于 2019-12-12 03:29:15

问题


Is it possible to use auto_detection for some bundles, while overriding the location of third-party models by specifying directories for them?

I've been playing with the JMSSerializerBundle the past couple of days trying to understand it and get it to work with a personal project, and it doesn't look like there is any way to use auto_detection as well as specify directories for third-party metadata.

If I write my FOSUserBundle Model.User.yml serialization data, e.g.:

FOS\UserBundle\Model\User:
    access_type: property
    exclusion_policy: ALL
    xml_root_name: user
    properties:
        username:
            type:     string
            expose:   true

And place it in the directory specified, while auto_detection is set to true, like so:

jms_serializer:
  metadata:
    debug: true
    file_cache:
      dir: "%kernel.cache_dir%/serializer"
    auto_detection: true
    directories:
      FOSUserBundle:
        namespace_prefix: FOS\UserBundle
        path: "@MyBundle/config/serializer/fosuser"

The serialization metadata is not picked up.

However, if I place the same metadata file under FOS/UserBundle/Resources/config/serializer (in the vendor bundle) and change the configuration to:

jms_serializer:
  metadata:
    debug: true
    file_cache:
      dir: "%kernel.cache_dir%/serializer"
    auto_detection: true
    #directories:
    #  FOSUserBundle:
    #    namespace_prefix: FOS\UserBundle
    #    path: "@MyBundle/config/serializer/fosuser"

The serialization metadata is read and properly applied.


回答1:


There was a typo in the directory path, it should have read:

jms_serializer:
  metadata:
    debug: true
    file_cache:
      dir: "%kernel.cache_dir%/serializer"
    auto_detection: true
    directories:
      FOSUserBundle:
        namespace_prefix: FOS\UserBundle
        path: "@MyBundle/Resources/config/serializer/fosuser"

Took me digging into the FileLocator to understand why the file was not being loaded.




回答2:


Modifying files in the vendors directory is a very bad practice. What you are looking for is answered here:

JMSSerializerBundle. no control over third party meta data

Kinda late though :p



来源:https://stackoverflow.com/questions/18796755/jmsserializerbundle-mixing-auto-detection-and-explicit-directories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!