this is more of a 'best practice' question than an actual problem:
I'm working on a project in Symfony 2 and I've built a bundle to handle all of my webservices. Basically one controller takes some JSON data, sends it off to another controller to check it matches a described format, then off to another controller to handle the database calls and eventually back to the initial controller to return a JSON response.
I'm sending data between controllers by doing something like this:
$controller = new \Acme\myBundle\Controller\DefaultController;
$response = $controller->function();
This works correctly, but I keep coming across one problem. In the controller I pass data to I need to instantiate AppKernel and call the boot function for any Symfony function to work. This to me seems a little bit silly which leads me to believe I may be doing this all wrong.
Any suggestions or tips appreciated!
EDIT/UPDATE Thank you all for the comments. I have set up my controllers as services, the services are working but I am still needing to manually boot/instantiate the kernel when I call a service from inside a service.
#config.yml
# API Services
services:
service.handler:
class: Acme\APIBundle\Controller\ServicesController
arguments:
container: "@service_container"
service.definitions:
class: Acme\APIBundle\Controller\DefinitionController
arguments:
container: "@service_container"
From another bundles controller I can then call a function from either of those services without problem:
$test = $this->get("service.handler")->testFunction();
$test2 = $this->get("service.definitions")->anotherTestFunction();
Where I DO have a problem is if I call a function inside of one service, then try to call another service from inside that service I get the following PHP error
Fatal error: Call to a member function get() on a non-object in /vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php on line 188
I can avoid that error by using this function and calling it rather than using $this
public function bootKernel(){
//boot kernel
$controller = new \Acme\myBundle\Controller\DefaultController;
$kernel = new \AppKernel('dev', true);$kernel->loadClassCache();
$kernel->boot();
$controller->setContainer($kernel->getContainer());
return($controller);
}
I guess my solution 'works' but it certainly doesn't seem an efficient way to do things.
EDIT 2: If I stick this at the top of the class then modify the service call it seems to work... Still not sure if this is the best way to do things.
protected $container;
public function __construct($container) {
$this->container= $container;
}