diff --git a/service_container.rst b/service_container.rst
index 90e642d6a97..137914dcd64 100644
--- a/service_container.rst
+++ b/service_container.rst
@@ -215,13 +215,19 @@ each time you ask for it.
.. code-block:: php
// app/config/services.php
- // _defaults and loading entire directories is not possible with PHP configuration
- // you need to define your services one-by-one
- use AppBundle\Service\MessageGenerator;
+ use Symfony\Component\DependencyInjection\Definition;
- $container->autowire(MessageGenerator::class)
+ // To use as default template
+ $definition = new Definition();
+
+ $definition
+ ->setAutowired(true)
->setAutoconfigured(true)
- ->setPublic(false);
+ ->setPublic(false)
+ ;
+
+ // $this is a reference to the current loader
+ $this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository}');
Thanks to this configuration, you can automatically use any classes from the
``src/AppBundle`` directory as a service, without needing to manually configure
@@ -475,12 +481,21 @@ pass here. No problem! In your configuration, you can explicitly set this argume
// app/config/services.php
use AppBundle\Updates\SiteUpdateManager;
+ use Symfony\Component\DependencyInjection\Definition;
- // _defaults and importing directories does not work in PHP
- // but registering a service explicitly does
- $container->autowire(SiteUpdateManager::class)
+ // Same as before
+ $definition = new Definition();
+
+ $definition
+ ->setAutowired(true)
->setAutoconfigured(true)
->setPublic(false)
+ ;
+
+ $this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository}');
+
+ // Explicitly configure the service
+ $container->getDefinition(SiteUpdateManager::class)
->setArgument('$adminEmail', 'manager@example.com');
.. versionadded:: 3.3
@@ -860,6 +875,31 @@ key. For example, the default Symfony configuration contains this:
+ .. code-block:: php
+
+ // app/config/services.php
+ use Symfony\Component\DependencyInjection\Definition;
+
+ // To use as default template
+ $definition = new Definition();
+
+ $definition
+ ->setAutowired(true)
+ ->setAutoconfigured(true)
+ ->setPublic(false)
+ ;
+
+ $this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository}');
+
+ // Changes default config
+ $definition
+ ->setPublic(true)
+ ->addTag('controller.service_arguments')
+ ;
+
+ // $this is a reference to the current loader
+ $this->registerClasses($definition, 'AppBundle\\Controller\\', '../../src/AppBundle/Controller/*');
+
This can be used to quickly make many classes available as services and apply some
default configuration. The ``id`` of each service is its fully-qualified class name.
You can override any service that's imported by using its id (class name) below
diff --git a/service_container/3.3-di-changes.rst b/service_container/3.3-di-changes.rst
index c9c9580cc54..1f21dd618d5 100644
--- a/service_container/3.3-di-changes.rst
+++ b/service_container/3.3-di-changes.rst
@@ -81,21 +81,37 @@ Symfony Standard Edition:
+
+
.. code-block:: php
// app/config/services.php
+ use Symfony\Component\DependencyInjection\Definition;
- // _defaults and loading entire directories is not possible with PHP configuration
- // you need to define your services one-by-one
- use AppBundle\Controller\DefaultController;
+ // To use as default template
+ $definition = new Definition();
- $container->autowire(DefaultController::class)
+ $definition
+ ->setAutowired(true)
->setAutoconfigured(true)
+ ->setPublic(false)
+ ;
+
+ $this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository}');
+
+ // Changes default config
+ $definition
+ ->setPublic(true)
->addTag('controller.service_arguments')
- ->setPublic(true);
+ ;
+
+ // $this is a reference to the current loader
+ $this->registerClasses($definition, 'AppBundle\\Controller\\', '../../src/AppBundle/Controller/*');
+
+ // add more services, or override services that need manual wiring
This small bit of configuration contains a paradigm shift of how services
are configured in Symfony.
@@ -147,9 +163,18 @@ thanks to the following config:
.. code-block:: php
// app/config/services.php
+ use Symfony\Component\DependencyInjection\Definition;
+
+ // To use as default template
+ $definition = new Definition();
- // services cannot be automatically loaded with PHP configuration
- // you need to define your services one-by-one
+ $definition
+ ->setAutowired(true)
+ ->setAutoconfigured(true)
+ ->setPublic(false)
+ ;
+
+ $this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository}');
This means that every class in ``src/AppBundle/`` is *available* to be used as a
service. And thanks to the ``_defaults`` section at the top of the file, all of
@@ -348,14 +373,12 @@ The third big change is that, in a new Symfony 3.3 project, your controllers are
// app/config/services.php
- // loading entire directories is not possible with PHP configuration
- // you need to define your services one-by-one
- use AppBundle\Controller\DefaultController;
+ // ...
+
+ // override default template
+ $definition->setPublic(true);
- $container->autowire(DefaultController::class)
- ->setAutoconfigured(true)
- ->addTag('controller.service_arguments')
- ->setPublic(true);
+ $this->registerClasses($definition, 'AppBundle\\Controller\\', '../../src/AppBundle/Controller/*');
But, you might not even notice this. First, your controllers *can* still extend
the same base ``Controller`` class or a new :ref:`AbstractController `.
@@ -642,7 +665,7 @@ You're now ready to automatically register all services in ``src/AppBundle/``
+ AppBundle\:
+ resource: '../../src/AppBundle/*'
+ exclude: '../../src/AppBundle/{Entity,Repository}'
- +
+ +
+ AppBundle\Controller\:
+ resource: '../../src/AppBundle/Controller'
+ public: true
diff --git a/service_container/configurators.rst b/service_container/configurators.rst
index ee906b86856..855344df981 100644
--- a/service_container/configurators.rst
+++ b/service_container/configurators.rst
@@ -166,20 +166,22 @@ all the classes are already loaded as services. All you need to do is specify th
.. code-block:: php
// app/config/services.php
- use AppBundle\Mail\EmailConfigurator;
- use AppBundle\Mail\EmailFormatterManager;
use AppBundle\Mail\GreetingCardManager;
use AppBundle\Mail\NewsletterManager;
+ use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
- // ...
- $container->autowire(EmailFormatterManager::class);
- $container->autowire(EmailConfigurator::class);
+ // Same as before
+ $definition = new Definition();
+
+ $definition->setAutowired(true);
+
+ $this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*');
- $container->autowire(NewsletterManager::class)
+ $container->getDefinition(NewsletterManager::class)
->setConfigurator(array(new Reference(EmailConfigurator::class), 'configure'));
- $container->autowire(GreetingCardManager::class)
+ $container->getDefinition(GreetingCardManager::class)
->setConfigurator(array(new Reference(EmailConfigurator::class), 'configure'));