我正在使用 symfony 3.3 的最新版本
我正在尝试返回 json,但出现错误
这是我的控制器:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Serializer\SerializerInterface;
class ApiController extends Controller{
/**
* @Route("home", name="api_home")
*/
public function indexAction(SerializerInterface $serializer)
{
$entity = $this->getDoctrine()
->getRepository('AppBundle:User')
->findAll();
$json = $serializer->serialize($entity,'json', ['groups' => ['User']]);
return new JsonResponse($json, 200, [], true);
}
}
在 services.yml 上:
services:
_defaults:
public: false
autowire: false
autoconfigure: true
配置.yml
serializer:
enabled: true
enable_annotations: true
我收到错误:
Controller "AppBundle\Controller\ApiController::indexAction()" requires that you provide a value for the "$serializer" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.
我试着在 $json 之前死掉
但同样的错误
谢谢