4

I'm developing an api with symphony a fosrestbundle. I also want to use constructor injection in my controllers like I always do to inject some EntityRepository.

The problem is that this injection is not working, I get an error that says that a repository was expected but none was given.

I figured out something: FOSRestBundle generates some url automatically, but if I want to use constructor injection, this urls have to use the name of the service I created for my controller. For instance, if my controller service is defined like this:

services.yml

dnd_agent_controller:
    class: DnD\RaHApiBundle\Controller\AgentController
    arguments: ["@dnd_agent_repository"]

Then my routes have to be defined like this:

routing.yml

agents_resource:
  path: /agents
  defaults: { _controller: dnd_agent_controller:getallAction}

Notice the _controller field, it's using the service name defined above, not the namespacebundle:controller:action like always.

With this configuration, my constructor injection works, however this is a problem for me since this urls are being generated automatically by the FOSRestBundle and I like that.

Is there a way to use both, constructor injection in my controllers and FOSRestBundle? How would I do it?

Thanks!

Community
  • 1
  • 1
danielrvt
  • 10,177
  • 20
  • 80
  • 121

1 Answers1

2

Yes, use FOSRestBundle routing style but with the service name.

Routing eg.

agents_resource:
    type: rest
    resource: dnd_agent_controller

EDIT: Blog post with example http://blog.dashboardhub.io/2015/09/26/symfony-controller-not-extending-fosrest-bundle/

Eddie Jaoude
  • 1,698
  • 15
  • 23