So, here is the controller I just built:
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; } public function retrieve($amount) { } }
I want this controller to work as a service to use DependencyInjection. So here is my services.xml file:
<?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>
However, when I try to execute my controller, I always get this exception:
Workaround fatal error: argument 1 passed to MDP \ API \ ImageBundle \ Controller \ RetrieverController :: __ construct () must be an instance of Symfony \ Component \ HttpFoundation \ JsonResponse not specified, called in / home / steve / projects / APIs / app / cache / dev / jms _diextra / controller_injectors / MDPAPIImageBundleControllerRetrieverController.php on line 13 and defined in /home/steve/projects/ImageAPI/ImageBundle/Controller/RetrieverController.php line 13
When I'm in dev mode, I see that Symfony generates this file in cached files ...
class RetrieverController__JMSInjector { public static function inject($container) { $instance = new \MDP\API\ImageBundle\Controller\RetrieverController(); return $instance; } }
How can I make sure that the arguments are correctly added to the controller, as indicated in my services.xml ?
source share