我如何在 Symfony 4 中使用 datetimeNormalizer?
How can I use datetimeNormalizer in Symfony 4?
这是我的控制器:
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
public function index($slug, Request $request, SerializerInterface $serializer)
{
$table = $this->getDoctrine()->getRepository($EntityName)->findAll();
$serializer = new Serializer(array(new DateTimeNormalizer('d.m.Y'), new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
$data = $serializer->serialize($table, 'json');
return $this->render('index.html.twig', ['data' => $data]);
}
它运行良好,但我收到警告:
User Deprecated: Passing configuration options directly to the
constructor is deprecated since Symfony 4.2, use the default context
instead.
问题在于您如何构建序列化程序,更具体地说是 DateTimeNormalizer。只要没有提供上下文,您就可以看到 error being triggered in the constructor of that class。
简单的解决方法是传入一个数组作为第一个参数:
new Serializer(
array(
new DateTimeNormalizer(array('datetime_format' => 'd.m.Y')),
new GetSetMethodNormalizer()
),
array(
'json' => new JsonEncoder()
)
);
所以只需将 d.m.Y
替换为 array('datetime_format' => 'd.m.Y')
。
既然你传入了一个 SerializerInterface,你可能想在你的 services.yaml 中配置你的序列化器:
services:
_defaults:
... # other default settings
bind:
Symfony\Component\Serializer\SerializerInterface $dateSerializer: '@app.date_serializer'
Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer: ~
Symfony\Component\Serializer\Encoder\JsonEncoder: ~
app.date_normalizer:
class: Symfony\Component\Serializer\Normalizer\DateTimeNormalizer
arguments:
- { 'datetime_format': 'd.m.Y' }
app.date_serializer:
class: Symfony\Component\Serializer\Serializer
arguments:
- ['@app.date_normalizer', '@Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer']
- ['@Symfony\Component\Serializer\Encoder\JsonEncoder']
在你的控制器中,你可以通过更改参数的名称来注入你的序列化程序
public function index($slug, Request $request, SerializerInterface $dateSerializer)
事实上,无论你想重用那个序列化器,你都可以通过 SerializerInterface $dateSerializer
注入它来获得它,这要归功于你的服务配置中的绑定。
旁注绑定仅适用于 Symfony 4.2。在旧版本中,您必须删除开头的类型(SerializerInterface),因为这是一项新功能。我认为所有其他东西都应该适用于 Symfony 3.4 和 4.0。
这是我的控制器:
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
public function index($slug, Request $request, SerializerInterface $serializer)
{
$table = $this->getDoctrine()->getRepository($EntityName)->findAll();
$serializer = new Serializer(array(new DateTimeNormalizer('d.m.Y'), new GetSetMethodNormalizer()), array('json' => new JsonEncoder()));
$data = $serializer->serialize($table, 'json');
return $this->render('index.html.twig', ['data' => $data]);
}
它运行良好,但我收到警告:
User Deprecated: Passing configuration options directly to the constructor is deprecated since Symfony 4.2, use the default context instead.
问题在于您如何构建序列化程序,更具体地说是 DateTimeNormalizer。只要没有提供上下文,您就可以看到 error being triggered in the constructor of that class。
简单的解决方法是传入一个数组作为第一个参数:
new Serializer(
array(
new DateTimeNormalizer(array('datetime_format' => 'd.m.Y')),
new GetSetMethodNormalizer()
),
array(
'json' => new JsonEncoder()
)
);
所以只需将 d.m.Y
替换为 array('datetime_format' => 'd.m.Y')
。
既然你传入了一个 SerializerInterface,你可能想在你的 services.yaml 中配置你的序列化器:
services:
_defaults:
... # other default settings
bind:
Symfony\Component\Serializer\SerializerInterface $dateSerializer: '@app.date_serializer'
Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer: ~
Symfony\Component\Serializer\Encoder\JsonEncoder: ~
app.date_normalizer:
class: Symfony\Component\Serializer\Normalizer\DateTimeNormalizer
arguments:
- { 'datetime_format': 'd.m.Y' }
app.date_serializer:
class: Symfony\Component\Serializer\Serializer
arguments:
- ['@app.date_normalizer', '@Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer']
- ['@Symfony\Component\Serializer\Encoder\JsonEncoder']
在你的控制器中,你可以通过更改参数的名称来注入你的序列化程序
public function index($slug, Request $request, SerializerInterface $dateSerializer)
事实上,无论你想重用那个序列化器,你都可以通过 SerializerInterface $dateSerializer
注入它来获得它,这要归功于你的服务配置中的绑定。
旁注绑定仅适用于 Symfony 4.2。在旧版本中,您必须删除开头的类型(SerializerInterface),因为这是一项新功能。我认为所有其他东西都应该适用于 Symfony 3.4 和 4.0。