【发布时间】:2019-09-06 11:56:30
【问题描述】:
我正在使用 symfony 4.2 构建一个 Api,并希望在使用 jms-serializer 安装后以 Json 格式序列化我的数据
composer 需要 jms/serializer-bundle
当我尝试以这种方式使用它时:
``` demands = $demandRepo->findAll();
return $this->container->get('serializer')->serialize($demands,'json');```
它给了我这个错误:
Service "serializer" not found, the container inside "App\Controller\DemandController" is a smaller service locator that only knows about the "doctrine", "http_kernel", "parameter_bag", "request_stack", "router" and "session" services. Try using dependency injection instead.
【问题讨论】:
-
离题。为什么要使用 JMSSerializer 而不是 Symfony 的默认序列化程序?顺便说一句,执行 php /bin/console debug:container 并检查服务是否存在。对于其他部分,最佳实践是由构造函数注入序列化程序。
-
不使用 Symfony 序列化程序的原因是:它让我厌倦了对象的依赖关系 :'(。我现在就试试。
标签: symfony jmsserializerbundle jsonresponse symfony-4.2