简体   繁体   English

Symfony 序列化程序配置 yaml

[英]Symfony serializer configure yaml

I have my custom service, eg.我有我的定制服务,例如。 MyService with serializer MyService与序列化程序

<?php
    
declare(strict_types=1);
    
namespace App\Common;
    
use Symfony\Component\Serializer\SerializerInterface;
    
class MyService
{
    public function __construct(private readonly SerializerInterface $serializer)
    {
    }
}

I need to use serializer like this:我需要像这样使用序列化程序:

new Serializer(
    normalizers: [
        new ObjectNormalizer(propertyTypeExtractor: new ReflectionExtractor()),
    ],
    encoders: [new JsonEncoder()]
);

How should i configure that via services.yaml ?我应该如何通过services.yaml配置它?

    App\Common\MyService:
        arguments:
            $serializer: '@serializer' #????

fixed: i created new serializer component固定:我创建了新的序列化程序组件

final class MySerializer extends \Symfony\Component\Serializer\Serializer
{
    public function __construct()
    {
        $normalizers = [
            new ObjectNormalizer(propertyTypeExtractor: new ReflectionExtractor()),
        ];
        $encoders = [new JsonEncoder()];
        parent::__construct($normalizers, $encoders);
    }
}

and inject into my service...并注入我的服务...

class MyService
{
    public function __construct(private readonly MySerializer $serializer)
    {
    }
}

without configs... looks like symfony style...没有配置...看起来像 symfony 风格...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM