Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. Components
  4. Dependency Injection
  5. Advanced Container Configuration
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Table of Contents

  • Marking Services as Public / Private
  • Aliasing
  • Decorating Services

Advanced Container Configuration

Edit this page

Warning: You are browsing the documentation for Symfony 2.6, which is no longer maintained.

Read the updated version of this page for Symfony 6.3 (the current stable version).

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:

1
$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):

1
2
3
4
services:
   foo:
     class: Example\Foo
     public: false
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>
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:

1
$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.

1
2
3
4
5
services:
   foo:
     class: Example\Foo
   bar:
     alias: foo
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>
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:

1
$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"

Decorating Services

2.5

Decorated services were introduced in Symfony 2.5.

When overriding an existing definition, the old service is lost:

1
2
3
4
5
$container->register('foo', 'FooService');

// this is going to replace the old definition with the new one
// old definition is lost
$container->register('foo', 'CustomFooService');

Most of the time, that's exactly what you want to do. But sometimes, you might want to decorate the old one instead. In this case, the old service should be kept around to be able to reference it in the new one. This configuration replaces foo with a new one, but keeps a reference of the old one as bar.inner:

1
2
3
4
5
bar:
  public: false
  class: stdClass
  decorates: foo
  arguments: ["@bar.inner"]
1
2
3
<service id="bar" class="stdClass" decorates="foo" public="false">
    <argument type="service" id="bar.inner" />
</service>
1
2
3
4
5
6
use Symfony\Component\DependencyInjection\Reference;

$container->register('bar', 'stdClass')
    ->addArgument(new Reference('bar.inner'))
    ->setPublic(false)
    ->setDecoratedService('foo');

Here is what's going on here: the setDecoratedService() method tells the container that the bar service should replace the foo service, renaming foo to bar.inner. By convention, the old foo service is going to be renamed bar.inner, so you can inject it into your new service.

Note

The generated inner id is based on the id of the decorator service (bar here), not of the decorated service (foo here). This is mandatory to allow several decorators on the same service (they need to have different generated inner ids).

Most of the time, the decorator should be declared private, as you will not need to retrieve it as bar from the container. The visibility of the decorated foo service (which is an alias for bar) will still be the same as the original foo visibility.

You can change the inner service name if you want to:

1
2
3
4
5
6
bar:
  class: stdClass
  public: false
  decorates: foo
  decoration_inner_name: bar.wooz
  arguments: ["@bar.wooz"]
1
2
3
<service id="bar" class="stdClass" decorates="foo" decoration-inner-name="bar.wooz" public="false">
    <argument type="service" id="bar.wooz" />
</service>
1
2
3
4
5
6
use Symfony\Component\DependencyInjection\Reference;

$container->register('bar', 'stdClass')
    ->addArgument(new Reference('bar.wooz'))
    ->setPublic(false)
    ->setDecoratedService('foo', 'bar.wooz');
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:
    Online exam, become Sylius certified today

    Online exam, become Sylius certified today

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Symfony footer

    ↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

    Avatar of Pavel Máca, a Symfony contributor

    Thanks Pavel Máca for being a Symfony contributor

    2 commits • 14 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • Symfony at a Glance
      • Symfony Components
      • Case Studies
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • SymfonyConnect
      • Support
      • How to be Involved
      • Code of Conduct
      • Events & Meetups
      • Projects using Symfony
      • Downloads Stats
      • Contributors
      • Backers
    • Blog

      • Events & Meetups
      • A week of symfony
      • Case studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Documentation
      • Living on the edge
      • Releases
      • Security Advisories
      • SymfonyInsight
      • Twig
      • SensioLabs
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Deployed on

    Follow Symfony

    Search by Meilisearch