Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DependencyInjection] Docs for method autowiring #7041

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
87 changes: 87 additions & 0 deletions components/dependency_injection/autowiring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -392,5 +392,92 @@ As for other RAD features such as the FrameworkBundle controller or annotations,
keep in mind to not use autowiring in public bundles nor in large projects with
complex maintenance needs.

Method injection
----------------

By default, the autowiring subsystem only injects dependencies in the constructor of
the service. Constructor injection should always be preferred; however, it is sometimes
convenient to inject dependencies through methods (usually a setter method).

.. versionadded:: 2.8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be 3.2

Method autowiring was added in Symfony 3.2

The ``*`` character can be used to ask the autowiring subsystem to inject dependencies
both in the constructor and in setter methods (starting with ``set``).

.. configuration-block::

.. code-block:: yaml

services:
foo:
class: Foo
autowire: '*'

.. code-block:: xml

<?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="Foo" autowire="*" />
</services>
</container>

.. code-block:: php

use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Definition;

// ...
$fooDefinition = new Definition('Foo');
$fooDefinition->setAutowired('*');

$container->setDefinition('foo', $fooDefinition);

Alternatively, the list of method to use for dependency injection can be configured explicitly:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

methods


.. configuration-block::

.. code-block:: yaml

services:
foo:
class: Foo
autowire: ['__construct', 'setFoo', 'notASetter']

.. code-block:: xml

<?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="Foo">
<autowire>__construct</autowire>
<autowire>setFoo</autowire>
<autowire>notASetter</autowire>
</service>
</services>
</container>

.. code-block:: php

use Symfony\Component\DependencyInjection\Reference;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not used.

use Symfony\Component\DependencyInjection\Definition;

// ...
$fooDefinition = new Definition('Foo');
$fooDefinition->setAutowired(array('__construct', 'setFoo', 'notASetter'));

$container->setDefinition('foo', $fooDefinition);


When listing methods to use, dependencies are not automatically injected in the constructor,
it needs to be listed explicitly.

.. _Rapid Application Development: https://en.wikipedia.org/wiki/Rapid_application_development
.. _ROT13: https://en.wikipedia.org/wiki/ROT13