Skip to content

Commit 3e0ea84

Browse files
committed
minor #20091 [DependencyInjection] Update import.rst (GrinWay)
This PR was submitted for the 7.1 branch but it was merged into the 6.4 branch instead. Discussion ---------- [DependencyInjection] Update import.rst It's better to use imports statements only in the services.yaml file. The following explains that. <!-- If your pull request fixes a BUG, use the oldest maintained branch that contains the bug (see https://symfony.com/releases for the list of maintained branches). If your pull request documents a NEW FEATURE, use the same Symfony branch where the feature was introduced (and `7.x` for features of unreleased versions). --> Commits ------- f20060c Update import.rst
2 parents efffcc8 + f20060c commit 3e0ea84

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

service_container/import.rst

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,28 @@ as in the above example, the ``App\`` definition creates services for classes
138138
found in ``../src/*``. If your imported file defines services for those classes
139139
too, they will be overridden.
140140

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.
141+
There are exactly three possible solutions in order services not to get overriden:
142+
1. Include the file with ``App\`` statement in the ``imports`` as the first element.
143+
In order to the fact that the ``imports`` statement not override existing services, it checks if the services exists,
144+
also take into account that the last element of the ``imports`` has the highest priority and will be executed first,
145+
having included ``App\`` as a first element of ``imports`` (with the lowest priority) it will be imported in the end.
146+
And being the last import element it will only add not existing services in the container.
147+
2. Include the path to the service in the ``exclude`` section.
148+
3. Write service definitions down the ``App\`` statement to override it
149+
150+
It's recommended to use the 1st approach to define services in the container
151+
Using the first approach the whole ``services.yaml`` file will look the foolowing way:
152+
153+
.. configuration-block::
154+
.. code-block:: yaml
155+
###> imports are loaded first (imports not overrides existing services) ###
156+
imports:
157+
- resource: 'services_yaml/resource_services.yaml' # PRIORITY 1 (last) (contains App\ with resource statement)
158+
- resource: 'services_yaml/services/' # PRIORITY 2
159+
- resource: 'services_yaml/parameters/' # PRIORITY 3 (first)
160+
161+
###> then services.yaml (what below overrides imports) ###
162+
###>... it's better to use only imports
145163

146164
.. include:: /components/dependency_injection/_imports-parameters-note.rst.inc
147165

0 commit comments

Comments
 (0)