Skip to content

Commit ab86f7f

Browse files
committed
Reword
1 parent 4bb5153 commit ab86f7f

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

service_container/import.rst

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Importing Configuration with ``imports``
3131
----------------------------------------
3232

3333
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
3535
decided to move some configuration to a new file:
3636

3737
.. configuration-block::
@@ -70,7 +70,8 @@ decided to move some configuration to a new file:
7070
// ... some parameters
7171
// ... some services
7272
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:
7475

7576
.. configuration-block::
7677

@@ -80,6 +81,18 @@ To import this file, use the ``imports`` key from a file that *is* loaded:
8081
imports:
8182
- { resource: services/mailer.yaml }
8283
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+
8396
.. code-block:: xml
8497
8598
<!-- config/services.xml -->
@@ -91,22 +104,44 @@ To import this file, use the ``imports`` key from a file that *is* loaded:
91104
92105
<imports>
93106
<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+
<!-- ... -->
94114
</imports>
95115
</container>
96116
97117
.. code-block:: php
98118
99119
// config/services.php
100-
$loader->import('services/mailer.php');
120+
use Symfony\Component\DependencyInjection\Definition;
101121
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');
106123
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.
110145

111146
.. include:: /components/dependency_injection/_imports-parameters-note.rst.inc
112147

0 commit comments

Comments
 (0)