WARNING: You are browsing the documentation for Symfony 2.3 which is not maintained anymore. Consider upgrading your projects to Symfony 4.1.

Advanced Container Configuration

2.3 version
Unmaintained

Advanced Container Configuration

Marking Services as Public / Private

When defining services, you'll usually want to be able to access these definitions within your application code. These services are called public. For example, the doctrine service registered with the container when using the DoctrineBundle is a public service. This means that you can fetch it from the container using the get() method:

$doctrine = $container->get('doctrine');

In some cases, a service only exists to be injected into another service and is not intended to be fetched directly from the container as shown above.

In these cases, to get a minor performance boost, you can set the service to be not public (i.e. private):

  • YAML
    1
    2
    3
    4
    services:
       foo:
         class: Example\Foo
         public: false
    
  • XML
    1
    2
    3
    4
    5
    6
    7
    8
    9
    <?xml version="1.0" encoding="UTF-8" ?>
    <container xmlns="http://symfony.com/schema/dic/services"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    
        <services>
            <service id="foo" class="Example\Foo" public="false" />
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    5
    use Symfony\Component\DependencyInjection\Definition;
    
    $definition = new Definition('Example\Foo');
    $definition->setPublic(false);
    $container->setDefinition('foo', $definition);
    

What makes private services special is that, if they are only injected once, they are converted from services to inlined instantiations (e.g. new PrivateThing()). This increases the container's performance.

Now that the service is private, you should not fetch the service directly from the container:

$container->get('foo');

This may or may not work, depending on if the service could be inlined. Simply said: A service can be marked as private if you do not want to access it directly from your code.

However, if a service has been marked as private, you can still alias it (see below) to access this service (via the alias).

Note

Services are by default public.

Aliasing

You may sometimes want to use shortcuts to access some services. You can do so by aliasing them and, furthermore, you can even alias non-public services.

  • YAML
    1
    2
    3
    4
    5
    services:
       foo:
         class: Example\Foo
       bar:
         alias: foo
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    <?xml version="1.0" encoding="UTF-8" ?>
    <container xmlns="http://symfony.com/schema/dic/services"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    
        <services>
            <service id="foo" class="Example\Foo" />
    
            <service id="bar" alias="foo" />
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    5
    use Symfony\Component\DependencyInjection\Definition;
    
    $container->setDefinition('foo', new Definition('Example\Foo'));
    
    $containerBuilder->setAlias('bar', 'foo');
    

This means that when using the container directly, you can access the foo service by asking for the bar service like this:

$container->get('bar'); // Would return the foo service

Tip

In YAML, you can also use a shortcut to alias a service:

1
2
3
4
services:
   foo:
     class: Example\Foo
   bar: '@foo'

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.