Symfony控制器作为服务无法正常工作

时间:2015-03-11 01:55:50

标签: symfony dependency-injection controller

我有这个控制器:

<?php

namespace DnD\RaHApiBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class testController extends Controller
{

  private $agentRepository;

  public function __construct(AgentRepository $agentRepository)
  {
    $this->agentRepository = $agentRepository;
  }


  public function getall()
  {
    return "asdf";
  }

}

此routing.yml:

test:
  path: /test
  defaults: {_controller: test_controller:getall}

这个services.yml

agent_repository:
        class: Doctrine\ORM\EntityRepository
        factory_service: doctrine.orm.default_entity_manager
        factory_method: getRepository
        arguments:
            - DnD\RaHApiBundle\Entity\Agent


test:
        class: DnD\RaHApiBundle\Controller\testController
        arguments: ["@agent_repository"]

我正在使用fosrestbundle开发API。每当我点击这个url:/ test时,我都会收到以下错误:

Catchable Fatal Error: Argument 1 passed to DnD\RaHApiBundle\Controller\testController::__construct() must be an instance of DnD\RaHApiBundle\Controller\AgentRepository, instance of DnD\RaHApiBundle\Repository\AgentRepository given, called in /Users/danielrvt/IdeaProjects/rentahouse/app/cache/dev/appDevDebugProjectContainer.php on line 2525 and defined

1 个答案:

答案 0 :(得分:1)

您忘记在控制器中添加use语句。

use DnD\RaHApiBundle\Repository\AgentRepository;