Skip to content

Commit 1cb10a0

Browse files
committed
"public: false" in service configuration file doesn't exist anymore
1 parent 667c179 commit 1cb10a0

File tree

2 files changed

+4
-11
lines changed

2 files changed

+4
-11
lines changed

service_container.rst

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,6 @@ each time you ask for it.
154154
_defaults:
155155
autowire: true # Automatically injects dependencies in your services.
156156
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
157-
public: false # Allows optimizing the container by removing unused services; this also means
158-
# fetching services directly from the container via $container->get() won't work.
159-
# The best practice is to be explicit about your dependencies anyway.
160157
161158
# makes classes in src/ available to be used as services
162159
# this creates a service per class whose id is the fully-qualified class name
@@ -177,7 +174,7 @@ each time you ask for it.
177174
178175
<services>
179176
<!-- Default configuration for services in *this* file -->
180-
<defaults autowire="true" autoconfigure="true" public="false"/>
177+
<defaults autowire="true" autoconfigure="true"/>
181178
182179
<prototype namespace="App\" resource="../src/*" exclude="../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}"/>
183180
</services>
@@ -815,8 +812,7 @@ loss, enable the compiler pass in your application.
815812
Public Versus Private Services
816813
------------------------------
817814

818-
Thanks to the ``_defaults`` section in ``services.yaml``, every service defined in
819-
this file is ``public: false`` by default.
815+
From Symfony 4.0, every service defined is private by default.
820816

821817
What does this mean? When a service **is** public, you can access it directly
822818
from the container object, which is accessible from any controller that extends
@@ -956,9 +952,7 @@ them will not cause the container to be rebuilt.
956952
.. note::
957953

958954
Wait, does this mean that *every* class in ``src/`` is registered as
959-
a service? Even model classes? Actually, no. As long as you have
960-
``public: false`` under your ``_defaults`` key (or you can add it under the
961-
specific import), all the imported services are *private*. Thanks to this, all
955+
a service? Even model classes? Actually, no. As long as you keep your imported services as :ref:`private <container-public>`, all
962956
classes in ``src/`` that are *not* explicitly used as services are
963957
automatically removed from the final container. In reality, the import
964958
means that all classes are "available to be *used* as services" without needing

service_container/alias_private.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ And in this case, those services do *not* need to be public.
2323

2424
So unless you *specifically* need to access a service directly from the container
2525
via ``$container->get()``, the best-practice is to make your services *private*.
26-
In fact, the :ref:`default services.yaml configuration <container-public>` configures
27-
all services to be private by default.
26+
In fact, All services are :ref:`private <container-public>` by default.
2827

2928
You can also control the ``public`` option on a service-by-service basis:
3029

0 commit comments

Comments
 (0)