Skip to content

[DependencyInjection] Update import.rst #20091

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

Merged
merged 1 commit into from
May 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions service_container/import.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,28 @@
found in ``../src/*``. If your imported file defines services for those classes
too, they will be overridden.

A possible solution for this is to add the classes and/or directories of the
imported files in the ``exclude`` option of the ``App\`` definition. Another
solution is to not use imports and add the service definitions in the same file,
but after the ``App\`` definition to override it.
There are exactly three possible solutions in order services not to get overriden:

Check failure on line 141 in service_container/import.rst

View workflow job for this annotation

GitHub Actions / Lint (DOCtor-RST)

Typo in word "overriden"
1. Include the file with ``App\`` statement in the ``imports`` as the first element.
In order to the fact that the ``imports`` statement not override existing services, it checks if the services exists,
also take into account that the last element of the ``imports`` has the highest priority and will be executed first,
having included ``App\`` as a first element of ``imports`` (with the lowest priority) it will be imported in the end.
And being the last import element it will only add not existing services in the container.
2. Include the path to the service in the ``exclude`` section.
3. Write service definitions down the ``App\`` statement to override it

It's recommended to use the 1st approach to define services in the container
Using the first approach the whole ``services.yaml`` file will look the foolowing way:

.. configuration-block::

Check failure on line 153 in service_container/import.rst

View workflow job for this annotation

GitHub Actions / Lint (DOCtor-RST)

Please add a blank line after ".. configuration-block::
.. code-block:: yaml

Check failure on line 154 in service_container/import.rst

View workflow job for this annotation

GitHub Actions / Lint (DOCtor-RST)

Please add a blank line after " .. code-block:: yaml
###> imports are loaded first (imports not overrides existing services) ###
imports:
- resource: 'services_yaml/resource_services.yaml' # PRIORITY 1 (last) (contains App\ with resource statement)
- resource: 'services_yaml/services/' # PRIORITY 2
- resource: 'services_yaml/parameters/' # PRIORITY 3 (first)

###> then services.yaml (what below overrides imports) ###
###>... it's better to use only imports

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

Expand Down