How to Create Service Aliases and Mark Services as Private

How to Create Service Aliases and Mark Services as Private

Marking Services as Public / Private

When defining a service, it can be made to be public or private. If a service is public, it means that you can access it directly from the container at runtime. For example, the doctrine service is a public service:

// only public services can be accessed in this way
$doctrine = $container->get('doctrine');

But typically, services are accessed using dependency injection. And in this case, those services do not need to be public.

So unless you specifically need to access a service directly from the container via $container->get(), the best-practice is to make your services private. In fact, the default services.yml configuration configures all services to be private by default.

You can also control the public option on a service-by-service basis:

  • YAML
    1
    2
    3
    4
    5
    services:
        # ...
    
        AppBundle\Service\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="AppBundle\Service\Foo" public="false" />
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    use AppBundle\Service\Foo;
    
    $container->register(Foo::class)
        ->setPublic(false);
    

Private services are special because they allow the container to optimize whether and how they are instantiated. This increases the container's performance. It also gives you better errors: if you try to reference a non-existent service, you will get a clear error when you refresh any page, even if the problematic code would not have run on that page.

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

use AppBundle\Service\Foo;

$container->get(Foo::class);

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, but it's considered a good practice to mark as many services private as possible.

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
    6
    7
    8
    services:
        # ...
        AppBundle\Mail\PhpMailer:
            public: false
    
        app.mailer:
            alias: AppBundle\Mail\PhpMailer
            public: true
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    <?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="AppBundle\Mail\PhpMailer" public="false" />
    
            <service id="app.mailer" alias="AppBundle\Mail\PhpMailer" />
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    5
    6
    use AppBundle\Mail\PhpMailer;
    
    $container->register(PhpMailer::class)
        ->setPublic(false);
    
    $container->setAlias('app.mailer', PhpMailer::class);
    

This means that when using the container directly, you can access the PhpMailer service by asking for the app.mailer service like this:

$container->get('app.mailer'); // Would return a PhpMailer instance

Tip

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

1
2
3
services:
    # ...
    app.mailer: '@AppBundle\Mail\PhpMailer'

Anonymous Services

Note

Anonymous services are only supported by the XML and YAML configuration formats.

New in version 3.3: The feature to configure anonymous services in YAML was introduced in Symfony 3.3.

In some cases, you may want to prevent a service being used as a dependency of other services. This can be achieved by creating an anonymous service. These services are like regular services but they don't define an ID and they are created where they are used.

The following example shows how to inject an anonymous service into another service:

  • YAML
    1
    2
    3
    4
    5
    6
    7
    8
    9
    # app/config/services.yml
    services:
        _defaults:
            autowire: true
    
        AppBundle\Foo:
            arguments:
                - !service
                    class: AppBundle\AnonymousBar
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    <!-- app/config/services.xml -->
    <?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>
            <defaults autowire="true" />
    
            <service id="foo" class="AppBundle\Foo">
                <argument type="service">
                    <service class="AppBundle\AnonymousBar" />
                </argument>
            </service>
        </services>
    </container>
    

Using an anonymous service as a factory looks like this:

  • YAML
    1
    2
    3
    4
    5
    6
    7
    # app/config/services.yml
    services:
        _defaults:
            autowire: true
    
        AppBundle\Foo:
            factory: [ !service { class: AppBundle\FooFactory }, 'constructFoo' ]
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    <!-- app/config/services.xml -->
    <?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>
            <defaults autowire="true" />
    
            <service id="foo" class="AppBundle\Foo">
                <factory method="constructFoo">
                    <service class="App\FooFactory"/>
                </factory>
            </service>
        </services>
    </container>
    

Deprecating Services

Once you have decided to deprecate the use of a service (because it is outdated or you decided not to maintain it anymore), you can deprecate its definition:

  • YAML
    1
    2
    AppBundle\Service\OldService:
        deprecated: The "%service_id%" service is deprecated since 2.8 and will be removed in 3.0.
    
  • 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="AppBundle\Service\OldService">
                <deprecated>The "%service_id%" service is deprecated since 2.8 and will be removed in 3.0.</deprecated>
            </service>
        </services>
    </container>
    
  • PHP
    1
    2
    3
    4
    5
    6
    7
    8
    9
    use AppBundle\Service\OldService;
    
    $container
        ->register(OldService::class)
        ->setDeprecated(
            true,
            'The "%service_id%" service is deprecated since 2.8 and will be removed in 3.0.'
        )
    ;
    

Now, every time this service is used, a deprecation warning is triggered, advising you to stop or to change your uses of that service.

The message is actually a message template, which replaces occurrences of the %service_id% placeholder by the service's id. You must have at least one occurrence of the %service_id% placeholder in your template.

Note

The deprecation message is optional. If not set, Symfony will show this default message: The "%service_id%" service is deprecated. You should stop using it, as it will soon be removed..

Tip

It is strongly recommended that you define a custom message because the default one is too generic. A good message informs when this service was deprecated, until when it will be maintained and the alternative services to use (if any).

For service decorators (see How to Decorate Services), if the definition does not modify the deprecated status, it will inherit the status from the definition that is decorated.

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