@@ -31,7 +31,7 @@ Importing Configuration with ``imports``
31
31
----------------------------------------
32
32
33
33
By default, service configuration lives in ``config/services.yaml ``. But if that
34
- file becomes large, you're free to organize into multiple files. For suppose you
34
+ file becomes large, you're free to organize into multiple files. Suppose you
35
35
decided to move some configuration to a new file:
36
36
37
37
.. configuration-block ::
@@ -70,7 +70,8 @@ decided to move some configuration to a new file:
70
70
// ... some parameters
71
71
// ... some services
72
72
73
- To import this file, use the ``imports `` key from a file that *is * loaded:
73
+ To import this file, use the ``imports `` key from any other file and pass either
74
+ a relative or absolute path to the imported file:
74
75
75
76
.. configuration-block ::
76
77
@@ -80,6 +81,18 @@ To import this file, use the ``imports`` key from a file that *is* loaded:
80
81
imports :
81
82
- { resource: services/mailer.yaml }
82
83
84
+ services :
85
+ _defaults :
86
+ autowire : true
87
+ autoconfigure : true
88
+ public : false
89
+
90
+ App\ :
91
+ resource : ' ../src/*'
92
+ exclude : ' ../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
93
+
94
+ # ...
95
+
83
96
.. code-block :: xml
84
97
85
98
<!-- config/services.xml -->
@@ -91,22 +104,44 @@ To import this file, use the ``imports`` key from a file that *is* loaded:
91
104
92
105
<imports >
93
106
<import resource =" services/mailer.xml" />
107
+
108
+ <defaults autowire =" true" autoconfigure =" true" public =" false" />
109
+
110
+ <prototype namespace =" App\" resource =" ../src/*"
111
+ exclude =" ../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}" />
112
+
113
+ <!-- ... -->
94
114
</imports >
95
115
</container >
96
116
97
117
.. code-block :: php
98
118
99
119
// config/services.php
100
- $loader->import('services/mailer.php') ;
120
+ use Symfony\Component\DependencyInjection\Definition ;
101
121
102
- The ``resource `` location, for files, is either a relative path from the
103
- current file or an absolute path.
104
-
105
- .. caution ::
122
+ $loader->import('services/mailer.php');
106
123
107
- The imported files are loaded first; You should be careful, because it can
108
- lead to some of your services definition to be overridden by rules present
109
- inside the file where you do imports
124
+ $definition = new Definition();
125
+ $definition
126
+ ->setAutowired(true)
127
+ ->setAutoconfigured(true)
128
+ ->setPublic(false)
129
+ ;
130
+
131
+ $this->registerClasses($definition, 'App\\', '../src/*',
132
+ '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
133
+
134
+ When loading a configuration file, Symfony loads first the imported files and
135
+ then it processes the parameters and services defined in the file. If you use the
136
+ :ref: `default services.yaml configuration <service-container-services-load-example >`
137
+ as in the above example, the ``App\ `` definition creates services for classes
138
+ found in ``../src/* ``. If your imported file defines services for those classes
139
+ too, they will be overridden.
140
+
141
+ A possible solution for this is to add the classes and/or directories of the
142
+ imported files in the ``exclude `` option of the ``App\ `` definition. Another
143
+ solution is to not use imports and add the service definitions in the same file,
144
+ but after the ``App\ `` definition to override it.
110
145
111
146
.. include :: /components/dependency_injection/_imports-parameters-note.rst.inc
112
147
0 commit comments