我有一个控制器ApiBundle\Controller\UserController
,它包含以下内容:
<?php
namespace ApiBundle\Controller;
use Doctrine\ORM\EntityManager;
use FOS\RestBundle\Controller\Annotations\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\User;
use ApiBundle\Form\RegisterType;
use ApiBundle\Utility\ApiUtility;
class UserController extends ApiController
{
/**
* @var EntityManager $em
*/
private $em;
/**
* UserController constructor.
* @param EntityManager $entityManager
*/
function __construct(EntityManager $em)
{
$this->em = $em;
}
/**
* @Route("register")
* @Method({"POST"})
* @return \Symfony\Component\HttpFoundation\Response
* @param Request $request
*/
public function registerUser(Request $request)
{
$data = ApiUtility::getContentAsArray($request);
$user = new User();
$form = $this->createForm(new RegisterType(), $user);
$form->submit($data);
if($form->isValid()) {
$user->setUsername($request->get('email'));
$this->em->persist($user);
$this->em->flush();
}else{
return ApiUtility::returnResponse($form->getErrors());
}
}
}
它扩展了API控制器,如下所示:
<?php
namespace ApiBundle\Controller;
use FOS\RestBundle\Controller\FOSRestController;
class ApiController extends FOSRestController
{
}
它有以下服务:
services:
api.controller.user:
class: ApiBundle\Controller\UserController
public: false
parent: api.controller
arguments: ["@doctrine.orm.entity_manager"]
api.controller:
class: ApiBundle\Controller\ApiController
calls:
- [setContainer, ['@service_container']]
无论我做什么,我都无法注入容器。现在,当我试图通过构造函数将doctrine引入容器时,我得到Type error: Too few arguments to function ApiBundle\Controller\UserController::__construct(), 0 passed in app/cache/dev/classes.php on line 2230 and exactly 1 expected
,如果我将其切换回$ this-&gt; container-&gt; get('doctrine.orm.entity_manager')或getEntityManager,我得到容器是null。我怎样才能解决这个问题?