title: Access Service from Controller and/or Twig template
body: <p><em>Disclaimer: I'm slowly starting to get into Symfony and still have some problems understanding how the architecture works.</em></p>
  
  <p>Currently I set up different Bundles (Services, right?) that <em>should</em> deliver different output for different routes. So far I got around adding a simple Twig template that loads stylesheets and scripts via Assetics and Twig-blocks. Now I added another Bundle that queries data via Buzz from a remote location, which worked fine as a standalone script, but I don't get around printing output in a Twig template.</p>
  
  <p>The architecture of the original script is like the following (names made more generic):</p>
  
  <ul>
  <li><strong>Vendors</strong> - <code>abstract</code> class that serves as base for all remote request Bundles.</li>
  <li><strong>ServiceABC</strong> - <code>abstract</code> class that extends <code>Vendors</code> and defines Error handling and output preparation for the <em>ABC</em> service.</li>
  <li><strong>ClientXYZ</strong> - <code>final</code> class that extends <code>Service_ABC</code>, defines output parsing and normalization of the returned data.</li>
  </ul>
  
  <p>This Bundle got a <code>services.yml</code> file: </p>
  
  <pre><code># ~/MyApp/Bundle/ServiceABCBundle/Resources/config/services.yml
  parameters:
      service_abc_manager.class: MyApp\Bundle\ServiceABCBundle\Models\Service_ABC
      location_manager.class: MyApp\Bundle\ServiceABCBundle\Models\Clients\ClientLocation
      monitor_manager.class: MyApp\Bundle\ServiceABCBundle\Models\Clients\ClientMonitor
  
  services:
      service_abc_manager:
          abstract: true
      location_manager:
          class: %location_manager.class%
          parent: service_abc_manager
      monitor_manager:
          class: %monitor_manager.class%
          parent: service_abc_manager
  </code></pre>
  
  <p><em><sup>Names changed for easier reference - Typos by accident possible.</sup></em></p>
  
  <p>Now my <strong>problem/question</strong> is, that I don't really get behind the Symfony2 concept of how to get the output into the template.</p>
  
  <pre><code>namespace MyApp\Bundle\ServiceABCBundle\Controller;
  
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use MyApp\Bundle\ServiceABCBundle\Models\Clients\ClientLocation;
  
  class DefaultController extends Controller
  {
      public function indexAction()
      {
          $services = array();
          $services[] = $this-&gt;container-&gt;has('service_abc_manager');
          $services[] = $this-&gt;container-&gt;has('location_manager');
          $services[] = $this-&gt;container-&gt;has('client_location');
          $services[] = $this-&gt;container-&gt;has('ClientLocation');
          var_dump( $services );
          $client = new ClientLocation();
          var_dump( $client );
  
          $response = $this-&gt;render(
              'Service_ABC:Default:index.html.twig'
          );
          # $response-&gt;setCharset( 'utf-8' );
          # $response-&gt;headers-&gt;set( 'Content-Type', 'text/html' );
  
          return $response;
      }
  }
  </code></pre>
  
  <p>The output of the first <code>array()</code> named <code>$services</code> is always <code>false</code> and the <code>$client = new ClientLocation();</code> throws an Exception that the class name wasn't found.</p>
  
  <blockquote>
    <p>How can I access those Services/Bundle(parts)/Classes? And how would I render the output to a template?</p>
  </blockquote>
  
  <h2>Update</h2>
  
  <p>After I added the complete tree definition to <code>Configuration()-&gt;getConfigTreeBuilder()</code>, I'm able to see the definitions in the CLI:</p>
  
  <pre><code>class Configuration implements ConfigurationInterface
  {
      public function getConfigTreeBuilder()
      {
          $treeBuilder = new TreeBuilder();
          $rootNode = $treeBuilder-&gt;root( 'myapp_service_abc' );
          $rootNode
              -&gt;children()
                  -&gt;scalarNode('service_abc_manager')-&gt;end()
                  -&gt;scalarNode('location_manager')-&gt;end()
                  -&gt;scalarNode('monitor_manager')-&gt;end()
              -&gt;end()
          ;
          return $treeBuilder;
      }
  }
  </code></pre>
  
  <p>The CLI command <code>php app/console config:dump-reference myapp_service_abc</code> now gives me the following output:</p>
  
  <pre><code>myapp_service_abc:
      service_abc_manager:  ~
      location_manager:     ~
      monitor_manager:      ~
  </code></pre>
  
  <p>I can as well see that the config data was loaded, when I <code>var_dump( $loader );</code> inside <code>MyAppServiceABCExtension</code> right after <code>$loader-&gt;load( 'services.yml' );</code> was called.</p>
  
  <p>The output is the following:</p>
  
  <pre><code>object(Symfony\Component\DependencyInjection\Loader\YamlFileLoader)
    protected 'container' =&gt; 
      object(Symfony\Component\DependencyInjection\ContainerBuilder)
        private 'definitions' =&gt; 
          array
            'service_abc_manager' =&gt; 
              object(Symfony\Component\DependencyInjection\Definition)
            'location_manager' =&gt; 
              object(Symfony\Component\DependencyInjection\DefinitionDecorator)
                private 'parent' =&gt; string 'service_abc_manager'
            // etc.
  </code></pre>
  
  <p>The <strong>problem</strong> itself remains: There's still a <code>FALSE</code> return value inside <code>DefaultController()-&gt;indexAction()</code> when I <code>var_dump( $this-&gt;container-&gt;has( 'service_abc_manager' );</code>. I as well tried <code>var_dump( $this-&gt;container-&gt;has( 'location_manager' );</code> and <code>var_dump( $this-&gt;container-&gt;has( 'myapp.service_abc_manager' );</code> with the same result.</p>