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
  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
  • Synthetic Services
  • Aliasing
  • Requiring Files

Advanced Container Configuration

Edit this page

Warning: You are browsing the documentation for Symfony 2.4, 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 as you can access it via:

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

However, there are use-cases when you don't want a service to be public. This is common when a service is only defined because it could be used as an argument for another service.

Note

If you use a private service as an argument to only one other service, this will result in an inlined instantiation (e.g. new PrivateFooBar()) inside this other service, making it publicly unavailable at runtime.

Simply said: A service will be private when you do not want to access it directly from your code.

Here is an example:

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);

Now that the service is private, you cannot call:

1
$container->get('foo');

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.

Synthetic Services

Synthetic services are services that are injected into the container instead of being created by the container.

For example, if you're using the HttpKernel component with the DependencyInjection component, then the request service is injected in the ContainerAwareHttpKernel::handle() method when entering the request scope. The class does not exist when there is no request, so it can't be included in the container configuration. Also, the service should be different for every subrequest in the application.

To create a synthetic service, set synthetic to true:

1
2
3
services:
    request:
        synthetic: true
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="request" synthetic="true" />
    </services>
</container>
1
2
3
4
5
use Symfony\Component\DependencyInjection\Definition;

$container
    ->setDefinition('request', new Definition())
    ->setSynthetic(true);

As you see, only the synthetic option is set. All other options are only used to configure how a service is created by the container. As the service isn't created by the container, these options are omitted.

Now, you can inject the class by using Container::set:

1
2
// ...
$container->set('request', new MyRequest(...));

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"

Requiring Files

There might be use cases when you need to include another file just before the service itself gets loaded. To do so, you can use the file directive.

1
2
3
4
services:
   foo:
     class: Example\Foo\Bar
     file: "%kernel.root_dir%/src/path/to/file/foo.php"
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\Bar">
            <file>%kernel.root_dir%/src/path/to/file/foo.php</file>
        </service>
    </services>
</container>
1
2
3
4
5
use Symfony\Component\DependencyInjection\Definition;

$definition = new Definition('Example\Foo\Bar');
$definition->setFile('%kernel.root_dir%/src/path/to/file/foo.php');
$container->setDefinition('foo', $definition);

Notice that Symfony will internally call the PHP statement require_once, which means that your file will be included only once per request.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    Version:
    Become certified from home

    Become certified from home

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Symfony footer

    Avatar of Geert De Deckere, a Symfony contributor

    Thanks Geert De Deckere for being a Symfony contributor

    2 commits • 8 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