5

我正在尝试使用JMSSerializerBundle但运行后未列出该服务app/console container:debug | grep jms_serializer

$ app/console container:debug | grep jms_serializer
jms_serializer.json_deserialization_visitor   container JMS\SerializerBundle\Serializer\JsonDeserializationVisit
jms_serializer.json_serialization_visitor     container JMS\SerializerBundle\Serializer\JsonSerializationVisitor
jms_serializer.metadata.lazy_loading_driver   container Metadata\Driver\LazyLoadingDriver
jms_serializer.metadata_driver                container Metadata\Driver\DriverChain
jms_serializer.naming_strategy                container JMS\SerializerBundle\Serializer\Naming\CacheNamingStrate
jms_serializer.xml_deserialization_visitor    container JMS\SerializerBundle\Serializer\XmlDeserializationVisito
jms_serializer.xml_serialization_visitor      container JMS\SerializerBundle\Serializer\XmlSerializationVisitor
jms_serializer.yaml_serialization_visitor     container JMS\SerializerBundle\Serializer\YamlSerializationVisitor

安装后

composer require jms/serializer-bundle

并配置

// in AppKernel::registerBundles()
$bundles = array(
    // ...
    new JMS\SerializerBundle\JMSSerializerBundle($this),
    // ...
);

如果你在某个地方使用

$this->get('jms_serializer')

我得到错误You have requested a non-existent service "jms_serializer".

我该如何解决?

4

2 回答 2

4

服务的名称是序列化程序。您应该只使用以下内容。

$this->get('serializer');
于 2013-02-19T00:44:50.997 回答
0

捆绑包已安装但未在 bundles.php 中自动启用。必须手动启用它。

启用 JMSSerializerBundle :

JMS\SerializerBundle\JMSSerializerBundle::class => ['all' => true],

访问服务:

$serializer = $this->container->get('jms_serializer');

Symfony 4.4

于 2020-11-03T12:25:10.657 回答