Nicolas Grekas
Contributed by Nicolas Grekas in #21494

One of the most common issues when using autowiring in Symfony applications is having two or more services implementing the same interface. The autowiring subsystem cannot guess which one to use in each case and you end up seeing this error message:

1
2
[Symfony\Component\DependencyInjection\Exception\RuntimeException]
Unable to autowire argument of type "..." for the service "...".

This problem is solved using the autowiring_types option to define which is the default implementation to use:

1
2
3
4
5
6
7
# app/config/services.yml
services:
    annotations.reader:
        class: Doctrine\Common\Annotations\AnnotationReader
        autowiring-type: Doctrine\Common\Annotations\Reader
        public: false
    # ...

In Symfony 3.3 we decided to simplify this feature and autowiring-types are now deprecated. Instead, you must use simple service aliases:

1
2
3
4
5
6
7
8
9
# app/config/services.yml
services:
    annotations.reader:
        class: Doctrine\Common\Annotations\AnnotationReader
        public: false

    Doctrine\Common\Annotations\Reader:
        alias: annotations.reader
        public: false

It's recommended to create the new service aliases as private because you don't need them to be available in the compiled container.

Published in #Living on the edge