所以,这是我刚刚构建的控制器:
namespace MDP\API\ImageBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class RetrieverController {
private $jsonResponse;
private $request;
public function __construct(JsonResponse $jsonResponse, Request $request) {
$this->jsonResponse = $jsonResponse;
$this->request = $request;
}
/**
* @Route("/image/{amount}")
* @Template("MDPAPIImageBundle:Retriever:index.json.twig")
*/
public function retrieve($amount)
{
}
}
我想让这个控制器作为服务工作,使用 DependencyInjection。所以,这是我的services.xml
文件:
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="mdpapi_image.json_response" class="Symfony\Component\HttpFoundation\JsonResponse" />
<service id="mdpapi_image.request" class="Symfony\Component\HttpFoundation\Request" />
<service id="mdpapi_image.controller.retriever" class="MDP\API\ImageBundle\Controller\RetrieverController">
<argument type="service" id="mdpapi_image.json_response" />
<argument type="service" id="mdpapi_image.request" />
</service>
</services>
</container>
但是,当我尝试执行我的控制器时,我总是得到这个异常:
可捕获的致命错误:传递给 MDP\API\ImageBundle\Controller\RetrieverController::__construct() 的参数 1 必须是 Symfony\Component\HttpFoundation\JsonResponse 的实例,没有给出,在 /home/steve/projects/APIs/app 中调用/cache/dev/jms_diextra/controller_injectors/MDPAPIImageBundleControllerRetrieverController.php 在第 13 行并在 /home/steve/projects/ImageAPI/ImageBundle/Controller/RetrieverController.php 第 13 行定义
当我处于开发模式时,我看到 Symfony 在缓存文件中生成了这个文件......
class RetrieverController__JMSInjector
{
public static function inject($container) {
$instance = new \MDP\API\ImageBundle\Controller\RetrieverController();
return $instance;
}
}
我怎样才能使参数正确地添加到控制器中,就像在我的services.xml
文件中指定的那样?