简体   繁体   English

如何在服务 xml 文件中添加序列化程序?

[英]How to add serializer in the services xml file?

I am trying to use Serializer Component in shopware 6 I have create a Serializer Class which I want to inject as DI in the controller.我正在尝试在shopware 6中使用Serializer器组件我创建了一个串行器 Class,我想将其作为 DI 注入 controller 中。

Serializer.php串行器.php

class Serializer
{
    public function getSerializer(): Serializer
    {
        $encoder = [new JsonEncoder()];
        $normalizer = [new ObjectNormalizer()];

        return new Serializer($normalizer, $encoder);
    }

}

MyController.php MyController.php

public function __construct(Serializer $serializer)
{
    $this->serializer = $serializer;
}

My problem is how to include this serializer in my services.xml file我的问题是如何在我的services.xml文件中包含这个序列化程序

<service id="SwagMasterApi\Core\Api\MyController" public="true">

            <call method="setContainer">
                <argument type="service" id="service_container"/>
            </call>
<service>

Can anybody help.任何人都可以帮忙。 Thanks.谢谢。

You need to define your Serializer as a service and inject it as an argument to Controller.您需要将 Serializer 定义为服务并将其作为参数注入 Controller。 For instance, if FQCN of your serializer is SwagMasterApi\Service\Serializer .例如,如果您的序列化程序的 FQCN 是SwagMasterApi\Service\Serializer You need to add the following code into your services.xml您需要将以下代码添加到您的服务中。xml

<service id="SwagMasterApi\Service\Serializer">
<service>

<service id="SwagMasterApi\Core\Api\MyController" public="true">
    <argument type="service" id="SwagMasterApi\Service\Serializer" />
<service>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Symfony 串行器 XML 将自定义属性添加到根节点 - Symfony Serializer XML add custom attribute to root node Symfony串行器将带有子级的XML反序列化为对象 - Symfony Serializer Deserializing XML into Object with children 如何使用自定义 JMS 序列化程序处理程序设置 Nelmio Doc - How to set Nelmio Doc with a custom JMS Serializer Handler JmsSerializerBundle的安装以错误结束:“ jms_serializer.visitors”下的无法识别的选项“ xml” - JmsSerializerBundle installation ends with error: Unrecognized option “xml” under “jms_serializer.visitors” 如何在 controller 中定义和访问服务? - How to define services and access it in controller? 如何在Symfony中实例化自动连线服务? - How to instantiate autowired services in Symfony? 默认情况下,Symfony 服务是如何私有的? - How are Symfony services private by default? 如何使用FOS User Bundle和Symfony 4使用JMS序列化程序从用户实体中排除密码字段? - How to exclude password field from the user entity with JMS serializer using FOS User Bundle and Symfony 4? Symfony 4 服务在资源文件中声明时被忽略 - Symfony 4 services ignored when declared inside a resource file 无效的映射文件 BaseMedia.mongodb.xml - Invalid mapping file BaseMedia.mongodb.xml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM