Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'd go down the <a href="http://symfony.com/doc/current/cookbook/controller/service.html" rel="noreferrer">controller-as-a-service</a> route with the above. Say you have to call 3 methods as part of your processing. Let's call them <code>foo()</code>, <code>bar()</code> and <code>something()</code>. Each of these methods are in separate controllers:</p> <pre><code>namespace Acme\DemoBundle\Controller; class FooController { public function __construct($container, ...) { $this-&gt;container = $container; // ... deal with any more arguments etc here } public function foo($params) { // ... return $x; } protected function get($service) { return $this-&gt;container-&gt;get($service); } } </code></pre> <p>Ditto for the <code>bar()</code> and <code>something()</code> methods, each in their own controller. Then, add them to your application as a service. Eg in your <code>config.yml</code> (other methods available):</p> <pre><code>services: my.foo.service: class: Acme\FooBundle\Controller\FooController arguments: container: "@service_container" </code></pre> <p>See the Symfony docs for <a href="http://symfony.com/doc/current/book/service_container.html#creating-configuring-services-in-the-container" rel="noreferrer">more details</a> about how you can construct this entry, including injecting any dependencies such as an entity manager or similar. Now, you can get an instance of this from the container:</p> <pre><code>public function yourMainAction() { // ... $newData = $this-&gt;get("my.foo.service")-&gt;fooAction($someData); // ... return new Response(json_encode($newData), ...); } </code></pre> <p>Likewise again for <code>BarController</code> and <code>SomethingController</code>. The advantage now is that this service can be made available at any point in your application (whether via the container as above, or as an injected service), across bundles, without needing to instantiate the class manually yourself and provide any dependencies.</p> <p>For more information about the container etc, the Symfony docs have a <a href="http://symfony.com/doc/2.0/book/service_container.html" rel="noreferrer">good section</a> on it (referenced in the link above).</p> <p><strong>Edit</strong>: Adjusted the code example to include details of passing in arguments into the service. Also added a convenience <code>get()</code> method to retrieve services from the container.</p>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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