How can I fix a deprecated user in Symfony 4?

给你一囗甜甜゛ 提交于 2019-12-08 09:38:40

问题


I get the following error message:

User Deprecated: Passing configuration options directly to the constructor is deprecated since Symfony 4.2, use the default context instead.

This is the code Symfony is giving as trace for the problem:

$serializer = new Serializer(array(new DateTimeNormalizer('d.m.Y'), new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));

But I do not understand how to use a default context


回答1:


You have to use directly the service.

class DefaultController extends AbstractController
{
    public function index(SerializerInterface $serializer)
    {
        // keep reading for usage examples
    }
}

https://symfony.com/doc/current/serializer.html




回答2:


According to the documentation you have to use it like this:

-$serializer = new Serializer(array(new DateTimeNormalizer('d.m.Y'), new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
-$serializer->serialize($myObject, 'json')

+$serializer = new Serializer(array(new DateTimeNormalizer(), new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
+$serializer->serialize($myObject, 'json', [DateTimeNormalizer::FORMAT_KEY => 'd.m.Y'])


来源:https://stackoverflow.com/questions/53887667/how-can-i-fix-a-deprecated-user-in-symfony-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!