Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 721b227

Browse filesBrowse files
committed
minor #6504 Improved the docs for the DependencyInjection component (javiereguiluz)
This PR was squashed before being merged into the 2.3 branch (closes #6504). Discussion ---------- Improved the docs for the DependencyInjection component | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes | Applies to | 2.3+ | Fixed tickets | - Commits ------- 221ddf7 Improved the docs for the DependencyInjection component
2 parents 154eb93 + 221ddf7 commit 721b227
Copy full SHA for 721b227

File tree

Expand file treeCollapse file tree

2 files changed

+57
-15
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+57
-15
lines changed

‎components/dependency_injection/definitions.rst

Copy file name to clipboardExpand all lines: components/dependency_injection/definitions.rst
+54-14Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
Working with Container Service Definitions
55
==========================================
66

7+
Service definitions are the instructions describing how the container should
8+
build a service. They are not the actual services used by your applications
9+
710
Getting and Setting Service Definitions
811
---------------------------------------
912

@@ -32,34 +35,68 @@ with these methods and make changes to it these will be reflected in the
3235
container. If, however, you are creating a new definition then you can add
3336
it to the container using::
3437

35-
$container->setDefinition($id, $definition);
38+
use Symfony\Component\DependencyInjection\Definition;
39+
40+
$definition = new Definition('Acme\Service\MyService');
41+
$container->setDefinition('acme.my_service', $definition);
42+
43+
.. tip::
44+
45+
Registering service definitions is so common that the container provides a
46+
shortcut method called ``register()``::
47+
48+
$container->register('acme.my_service', 'Acme\Service\MyService');
3649

3750
Working with a Definition
3851
-------------------------
3952

4053
Creating a New Definition
4154
~~~~~~~~~~~~~~~~~~~~~~~~~
4255

43-
If you need to create a new definition rather than manipulate one retrieved
44-
from the container then the definition class is :class:`Symfony\\Component\\DependencyInjection\\Definition`.
56+
In addition to manipulating and retrieving existing definitions, you can also
57+
define new service definitions with the :class:`Symfony\\Component\\DependencyInjection\\Definition`
58+
class.
4559

4660
Class
4761
~~~~~
4862

49-
First up is the class of a definition, this is the class of the object returned
50-
when the service is requested from the container.
63+
The first optional argument of the ``Definition`` class is the fully qualified
64+
class name of the object returned when the service is fetched from the container::
5165

52-
To find out what class is set for a definition::
66+
use Symfony\Component\DependencyInjection\Definition;
67+
68+
$definition = new Definition('Acme\Service\MyService');
69+
70+
If the class is unknown when instantiating the ``Definition`` class, use the
71+
``setClass()`` method to set it later::
5372

54-
$definition->getClass();
73+
$definition->setClass('Acme\Service\MyService');
5574

56-
and to set a different class::
75+
To find out what class is set for a definition::
5776

58-
$definition->setClass($class); // Fully qualified class name as string
77+
$class = $definition->getClass();
78+
// $class = 'Acme\Service\MyService'
5979

6080
Constructor Arguments
6181
~~~~~~~~~~~~~~~~~~~~~
6282

83+
The second optional argument of the ``Definition`` class is an array with the
84+
arguments passed to the constructor of the object returned when the service is
85+
fetched from the container::
86+
87+
use Symfony\Component\DependencyInjection\Definition;
88+
89+
$definition = new Definition(
90+
'Acme\Service\MyService',
91+
array('argument1' => 'value1', 'argument2' => 'value2')
92+
);
93+
94+
If the arguments are unknown when instantiating the ``Definition`` class or if
95+
you want to add new arguments, use the ``addArgument()`` method, which adds them
96+
at the end of the arguments array::
97+
98+
$definition->addArgument($argument);
99+
63100
To get an array of the constructor arguments for a definition you can use::
64101

65102
$definition->getArguments();
@@ -69,12 +106,16 @@ or to get a single argument by its position::
69106
$definition->getArgument($index);
70107
// e.g. $definition->getArgument(0) for the first argument
71108

72-
You can add a new argument to the end of the arguments array using::
109+
The argument can be a string, an array or a service parameter by using the
110+
``%parameter_name%`` syntax::
73111

74-
$definition->addArgument($argument);
112+
$definition->addArgument('%kernel_debug%');
75113

76-
The argument can be a string, an array, a service parameter by using ``%parameter_name%``
77-
or a service id by using::
114+
If the argument is another service, don't use the ``get()`` method to fetch it,
115+
because it won't be available when defining services. Instead, use the
116+
:class:`Symfony\\Component\\DependencyInjection\\Reference` class to get a
117+
reference to the service which will be available once the service container is
118+
fully built::
78119

79120
use Symfony\Component\DependencyInjection\Reference;
80121

@@ -139,4 +180,3 @@ the service itself gets loaded. To do so, you can use the
139180

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

‎components/dependency_injection/introduction.rst

Copy file name to clipboardExpand all lines: components/dependency_injection/introduction.rst
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ like this::
104104
// ...
105105
}
106106

107-
Then you can register this as a service as well and pass the ``mailer`` service into it::
107+
When defining the ``newsletter_manager`` service, the ``mailer`` service does
108+
not exist yet. Use the ``Reference`` class to tell the container to inject the
109+
``mailer`` service when it initializes the newsletter manager::
108110

109111
use Symfony\Component\DependencyInjection\ContainerBuilder;
110112
use Symfony\Component\DependencyInjection\Reference;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.