0
У меня есть этот контроллер:Symfony контроллер в качестве службы не работает
<?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"]
Я развивающуюся API с fosrestbundle. Всякий раз, когда я ударил этот адрес:/тест, я получаю обратно следующее сообщение об ошибке:
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
Я хотел бы сказать, что ошибка очень extrange ... Я не мог понять, что из ... – danielrvt
К сожалению, я не знаю, что делает extrange значит. Когда вы опускаете инструкцию использования в любом классе и используете пространство имен, PHP добавляет это пространство имен в любой класс, расположенный в коде, кроме тех, которые определены в 'use' statement. Вот почему 'AgentRepository' в параметре конструктора становится' DnD \ RaHApiBundle \ Controller \ AgentRepository' –
Это верно для классов, у которых нет явных идентификаторов пространства имен. '\\' также является идентификатором пространства имен, который указывает на корневое пространство имен. –