问题
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