I have a REST API built with Symfony2 and the FOSRestBundle. This all works fine, however in one service I combine data form another service in a different bundle – this seemed like it would be simple but it is not.
I am creating a new request object and adding in my parameters, from there I fire off the request to the other service, the service receives the request fine, however, when I try to use $this->get it gives me the good old Call to a member function get() on a non-object in ...
I know that I am missing the service container (I don’t entirely understand why it’s available when I call a hit the first bundle but not the second), that’s all well and fine but how do I inject it or a component of it so I can use $this->get to hit my custom services defined in services.yml? (easy to pass them the service container using arguments:)
container: "@service_container"
Setting this bundle up as a service won’t work as FOSRestBundle does not call it as a service.
In Short: I want to be able to get data from bundle2 when inside bundle1 by doing
namespace MyVendor\Bundle1\Controller
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use MyVendor\Bundle2\Controller\Bundle2ClassName;
class Bundle1 {
//if i wanted to do this here it would work fine:
// $this->get('my.service.defined.in.service.yml');
$bundle2 = new Bundle2ClassName();
$returned_data = $bundle2->myFunction();
}
Then once inside myFunction in bundle2 if I try to call the exact same service function i get the dreaded get error. If I call bundle2 directly through the FOSRest route i obviously don’t have that problem.
namespace MyVendor\Bundle2\Controller
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
class Bundle2 {
//this does not work
$this->get('my.service.defined.in.service.yml');
//do some stuff then return response
return($response);
}
I’ve read all the service container docs over and over again, so if you’re going to link to them i’d appreciate it if you could point out the exact section where it explains how this stuff is handled. This has been the one problem i’ve never been able to fully understand since I started working with Symfony a few months back.
P.S. could someone with enough points add the FOSRestBundle as a tag?
Thanks!
First of all you should use
$this->forwardto forward request to another controller.Second, the reason you don’t have access to service container in second controller is probably because you’re trying to manually initialize it – never do that unless you absolutely know what you’re doing (specifically, you forgot to pass service container as controller dependency).
Third, just as an example on how things work – your original controller dependency on service container is handled by the same container and extends
ContainerAware, on that controller initialization asetContainer()is called, which you most likely forgot to do when manually initializing second controller. So to get it working (which again I strongly recommend not doing), you should do this:The reason you’re getting the
$this->get() on a non-object...error is because$this->get()in your controller is actually a shortcut to$this->container->get()(defined inSymfony\Bundle\FrameworkBundle\Controller\Controller)