如何修复 Symfony 4 中已弃用的用户?
How can I fix a deprecated user in Symfony 4?
我收到以下错误消息:
User Deprecated: Passing configuration options directly to the
constructor is deprecated since Symfony 4.2, use the default context
instead.
这是 Symfony 给出的跟踪问题的代码:
$serializer = new Serializer(array(new DateTimeNormalizer('d.m.Y'), new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
但我不明白如何使用默认上下文
您必须直接使用该服务。
class DefaultController extends AbstractController
{
public function index(SerializerInterface $serializer)
{
// keep reading for usage examples
}
}
根据 documentation 你必须这样使用它:
-$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'])
我检查了导致弃用的代码(DateTimeNormalizer class 的构造函数):
public function __construct($defaultContext = [], \DateTimeZone $timezone = null)
{
$this->defaultContext = [
self::FORMAT_KEY => \DateTime::RFC3339,
self::TIMEZONE_KEY => null,
];
if (!\is_array($defaultContext)) {
@trigger_error('Passing configuration options directly to the constructor is deprecated since Symfony 4.2, use the default context instead.', E_USER_DEPRECATED);
$defaultContext = [self::FORMAT_KEY => (string) $defaultContext];
$defaultContext[self::TIMEZONE_KEY] = $timezone;
}
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
}
所以基本上它归结为更改传递给 DateTimeNormalizer 的参数(在我的例子中我有一个 CompilerPass 确实设置了参数,但在下面的例子中我将保持它的通用性):
new DateTimeNormalizer('d.m.Y'); // old, causing the deprecation notice
// new and NOT causing the deprecation notice
new DateTimeNormalizer([
DateTimeNormalizer::FORMAT_KEY => 'd.m.Y'
]);
我收到以下错误消息:
User Deprecated: Passing configuration options directly to the constructor is deprecated since Symfony 4.2, use the default context instead.
这是 Symfony 给出的跟踪问题的代码:
$serializer = new Serializer(array(new DateTimeNormalizer('d.m.Y'), new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
但我不明白如何使用默认上下文
您必须直接使用该服务。
class DefaultController extends AbstractController
{
public function index(SerializerInterface $serializer)
{
// keep reading for usage examples
}
}
根据 documentation 你必须这样使用它:
-$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'])
我检查了导致弃用的代码(DateTimeNormalizer class 的构造函数):
public function __construct($defaultContext = [], \DateTimeZone $timezone = null)
{
$this->defaultContext = [
self::FORMAT_KEY => \DateTime::RFC3339,
self::TIMEZONE_KEY => null,
];
if (!\is_array($defaultContext)) {
@trigger_error('Passing configuration options directly to the constructor is deprecated since Symfony 4.2, use the default context instead.', E_USER_DEPRECATED);
$defaultContext = [self::FORMAT_KEY => (string) $defaultContext];
$defaultContext[self::TIMEZONE_KEY] = $timezone;
}
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
}
所以基本上它归结为更改传递给 DateTimeNormalizer 的参数(在我的例子中我有一个 CompilerPass 确实设置了参数,但在下面的例子中我将保持它的通用性):
new DateTimeNormalizer('d.m.Y'); // old, causing the deprecation notice
// new and NOT causing the deprecation notice
new DateTimeNormalizer([
DateTimeNormalizer::FORMAT_KEY => 'd.m.Y'
]);