试图在Symfony2中将我的控制器定义为服务 - 参数永远不会传递给构造函数

iRE*_*ful 6 php symfony

所以,这是我刚刚建立的控制器:

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)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

我想让这个控制器作为服务工作,使用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>
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试执行我的控制器时,我总是得到这个异常:

可捕获致命错误:传递给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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能使参数正确添加到控制器,就像在我的services.xml文件中指定的那样?

Eln*_*mov 0

似乎您忘记编写加载文件的扩展services.xml

namespace MDP\API\ImageBundle\DependencyInjection;

use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\Config\FileLocator;

class ImageExtension extends Extension
{
    /**
     * @param array $configs
     * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $loader = new XmlFileLoader(
            $container, 
            new FileLocator(__DIR__.'/../Resources/config')
        );

        $loader->load('services.xml');
    }
}
Run Code Online (Sandbox Code Playgroud)