[英]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 中。
class Serializer
{
public function getSerializer(): Serializer
{
$encoder = [new JsonEncoder()];
$normalizer = [new ObjectNormalizer()];
return new Serializer($normalizer, $encoder);
}
}
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.