New in Symfony 2.8: Service Auto Wiring

Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
Contributed by
Kévin Dunglas
in #15613.
The Dependency Injection component is one of the most important elements of the Symfony applications. This component allows developers to configure services in YAML, XML or PHP files and let Symfony create those services for them.
Services usually define an arguments
option listing the arguments passed to
their constructors. If the application contains the following two classes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
namespace AppBundle\Service;
class Service1
{
}
namespace AppBundle\Service;
use AppBundle\Service\Service1;
class Service2
{
private $service1;
public function __construct(Service1 $service1)
{
$this->service1 = $service1;
}
}
The needed YAML configuration would be the following:
1 2 3 4 5 6 7 8
# app/config/services.yml
services:
service1:
class: AppBundle\Service\Service1
service2:
class: AppBundle\Service\Service2
arguments: ['@service1']
In Symfony 2.8, thanks to the new service auto wiring feature, you can skip
the definition of service1
. The reason is that the service container is able
to introspect the constructor parameters, create a private service for Service1
class and inject it into service2
.
This feature is disabled by default and its behavior is restricted to the
cases where the services can be guessed unequivocally. You just need to set the
new autowire
option to true
in the services where you want auto wire and
let the service container do the rest.
This is how the same service configuration shown before looks like when using
auto wiring (service1
isn't defined explicitly and service2
doesn't
define its arguments):
1 2 3 4 5
# app/config/services.yml
services:
service2:
class: AppBundle\Service\Service2
autowire: true
Service auto wiring was pioneered more than 10 years ago by the Spring Java framework
with their @Autowired
annotation and it's a feature best suited for simplifying
the development of application prototypes.
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
should be "user AppBundle\Service\Service1"
"use" instead of "user"
Oskar
I'm not fan of this feature neither, but I understand the convenience for RAD.
+1 from me for this feature.
And if that's the case, then the use statement is even redundant.
Regarding the actual functionality, is it aware of aliases not actually being another service implementing the same class?
So, as the article correctly says: it's a feature for prototypes.
It is a nice feature for smaller projects where there are not that many services, but I agree it tends to be a bit too much magic.
i.e. What if there is a Service3 that depends on Service2 that depends on Service1?
Will only this be sufficient?
# app/config/services.yml
services:
service3:
class: AppBundle\Service\Service3
autowire: true
I used to (thanks to what i've learned from sensio team) configure routing with annotation and services as xml.
The bundle generator in 2.8 always set yml as services configuration… Is this format the new best practice ?