Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2 passing data between bundles & controllers
    primarykey
    data
    text
    <p>this is more of a 'best practice' question than an actual problem:</p> <p>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. </p> <p>I'm sending data between controllers by doing something like this:</p> <pre><code>$controller = new \Acme\myBundle\Controller\DefaultController; $response = $controller-&gt;function(); </code></pre> <p>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. </p> <p>Any suggestions or tips appreciated!</p> <p><strong>EDIT/UPDATE</strong> 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. </p> <pre><code>#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" </code></pre> <p>From another bundles controller I can then call a function from either of those services without problem: </p> <pre><code> $test = $this-&gt;get("service.handler")-&gt;testFunction(); $test2 = $this-&gt;get("service.definitions")-&gt;anotherTestFunction(); </code></pre> <p>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</p> <pre><code>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 </code></pre> <p>I can avoid that error by using this function and calling it rather than using $this</p> <pre><code> public function bootKernel(){ //boot kernel $controller = new \Acme\myBundle\Controller\DefaultController; $kernel = new \AppKernel('dev', true);$kernel-&gt;loadClassCache(); $kernel-&gt;boot(); $controller-&gt;setContainer($kernel-&gt;getContainer()); return($controller); } </code></pre> <p>I guess my solution 'works' but it certainly doesn't seem an efficient way to do things. </p> <p><strong>EDIT 2:</strong> 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. </p> <pre><code> protected $container; public function __construct($container) { $this-&gt;container= $container; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload