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:

  1. // only public services can be accessed in this way
  2. $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, All services are private by default.

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

  • YAML

    1. # config/services.yaml
    2. services:
    3. # ...
    4. App\Service\Foo:
    5. public: true
  • XML

    1. <!-- config/services.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
    6. <services>
    7. <service id="App\Service\Foo" public="true"/>
    8. </services>
    9. </container>
  • PHP

    1. // config/services.php
    2. namespace Symfony\Component\DependencyInjection\Loader\Configurator;
    3. use App\Service\Foo;
    4. return function(ContainerConfigurator $configurator) {
    5. $services = $configurator->services();
    6. $services->set(Foo::class)
    7. ->public();
    8. };

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:

  1. use App\Service\Foo;
  2. $container->get(Foo::class);

Thus, 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).

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. # config/services.yaml
    2. services:
    3. # ...
    4. App\Mail\PhpMailer:
    5. public: false
    6. app.mailer:
    7. alias: App\Mail\PhpMailer
    8. public: true
  • XML

    1. <!-- config/services.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/services
    6. https://symfony.com/schema/dic/services/services-1.0.xsd">
    7. <services>
    8. <service id="App\Mail\PhpMailer" public="false"/>
    9. <service id="app.mailer" alias="App\Mail\PhpMailer"/>
    10. </services>
    11. </container>
  • PHP

    1. // config/services.php
    2. namespace Symfony\Component\DependencyInjection\Loader\Configurator;
    3. use App\Mail\PhpMailer;
    4. return function(ContainerConfigurator $configurator) {
    5. $services = $configurator->services();
    6. $services->set(PhpMailer::class)
    7. ->private();
    8. $services->alias('app.mailer', PhpMailer::class);
    9. };

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

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

Tip

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

  1. # config/services.yaml
  2. services:
  3. # ...
  4. app.mailer: '@App\Mail\PhpMailer'

Deprecating Service Aliases

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

  • YAML

    1. app.mailer:
    2. alias: 'App\Mail\PhpMailer'
    3. # this will display a generic deprecation message...
    4. deprecated: true
    5. # ...but you can also define a custom deprecation message
    6. deprecated: 'The "%alias_id%" alias is deprecated. Do not use it anymore.'
  • XML

    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <container xmlns="http://symfony.com/schema/dic/services"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-Instance"
    4. xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
    5. <services>
    6. <service id="app.mailer" alias="App\Mail\PhpMailer">
    7. <!-- this will display a generic deprecation message... -->
    8. <deprecated/>
    9. <!-- ...but you can also define a custom deprecation message -->
    10. <deprecated>The "%alias_id%" service alias is deprecated. Don't use it anymore.</deprecated>
    11. </service>
    12. </services>
    13. </container>
  • PHP

    1. $container
    2. ->setAlias('app.mailer', 'App\Mail\PhpMailer')
    3. // this will display a generic deprecation message...
    4. ->setDeprecated(true)
    5. // ...but you can also define a custom deprecation message
    6. ->setDeprecated(
    7. true,
    8. 'The "%alias_id%" service alias is deprecated. Don\'t use it anymore.'
    9. )
    10. ;

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

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

New in version 4.3: The deprecated option for service aliases was introduced in Symfony 4.3.

Anonymous Services

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. # config/services.yaml
    2. services:
    3. App\Foo:
    4. arguments:
    5. - !service
    6. class: App\AnonymousBar
  • XML

    1. <!-- config/services.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/services
    6. https://symfony.com/schema/dic/services/services-1.0.xsd">
    7. <services>
    8. <service id="foo" class="App\Foo">
    9. <argument type="service">
    10. <service class="App\AnonymousBar"/>
    11. </argument>
    12. </service>
    13. </services>
    14. </container>
  • PHP

    1. // config/services.php
    2. namespace Symfony\Component\DependencyInjection\Loader\Configurator;
    3. use App\AnonymousBar;
    4. use App\Foo;
    5. return function(ContainerConfigurator $configurator) {
    6. $services = $configurator->services();
    7. $services->set(Foo::class)
    8. ->args([inline(AnonymousBar::class)]);
    9. };

Note

Anonymous services do NOT inherit the definitions provided from the defaults defined in the configuration. So you’ll need to explicitly mark service as autowired or autoconfigured when doing an anonymous service e.g.: inline(Foo::class)->autowire()->autoconfigure().

Using an anonymous service as a factory looks like this:

  • YAML

    1. # config/services.yaml
    2. services:
    3. App\Foo:
    4. factory: [ !service { class: App\FooFactory }, 'constructFoo' ]
  • XML

    1. <!-- config/services.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/services
    6. https://symfony.com/schema/dic/services/services-1.0.xsd">
    7. <services>
    8. <service id="foo" class="App\Foo">
    9. <factory method="constructFoo">
    10. <service class="App\FooFactory"/>
    11. </factory>
    12. </service>
    13. </services>
    14. </container>
  • PHP

    1. // config/services.php
    2. namespace Symfony\Component\DependencyInjection\Loader\Configurator;
    3. use App\AnonymousBar;
    4. use App\Foo;
    5. return function(ContainerConfigurator $configurator) {
    6. $services = $configurator->services();
    7. $services->set(Foo::class)
    8. ->factory([inline(AnonymousBar::class), 'constructFoo']);
    9. };

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. # config/services.yaml
    2. App\Service\OldService:
    3. deprecated: The "%service_id%" service is deprecated since vendor-name/package-name 2.8 and will be removed in 3.0.
  • XML

    1. <!-- config/services.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-Instance"
    5. xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
    6. <services>
    7. <service id="App\Service\OldService">
    8. <deprecated>The "%service_id%" service is deprecated since vendor-name/package-name 2.8 and will be removed in 3.0.</deprecated>
    9. </service>
    10. </services>
    11. </container>
  • PHP

    1. // config/services.php
    2. namespace Symfony\Component\DependencyInjection\Loader\Configurator;
    3. use App\Service\OldService;
    4. return function(ContainerConfigurator $configurator) {
    5. $services = $configurator->services();
    6. $services->set(OldService::class)
    7. ->deprecate('The "%service_id%" service is deprecated since vendor-name/package-name 2.8 and will be removed in 3.0.');
    8. };

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.