Skip to content

Commit 5452c61

Browse files
committed
Adding section about public: false
1 parent ee27765 commit 5452c61

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

service_container.rst

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,85 @@ it as a service. Then ``autoconfigure`` will add the ``twig.extension`` tag *for
748748
you, because your class implements ``Twig_ExtensionInterface``. And thanks to ``autowire``,
749749
you can even add constructor arguments without any configuration.
750750

751+
.. _container-public:
752+
753+
Public Versus Private Services
754+
------------------------------
755+
756+
Thanks to the ``_defaults`` section in ``services.yml``, every service defined in
757+
this file is ``public: false`` by default:
758+
759+
.. configuration-block::
760+
761+
.. code-block:: yaml
762+
763+
# app/config/services.yml
764+
services:
765+
# default configuration for services in *this* file
766+
_defaults:
767+
# ...
768+
public: false
769+
770+
.. code-block:: xml
771+
772+
<!-- app/config/services.xml -->
773+
<?xml version="1.0" encoding="UTF-8" ?>
774+
<container xmlns="http://symfony.com/schema/dic/services"
775+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
776+
xsi:schemaLocation="http://symfony.com/schema/dic/services
777+
http://symfony.com/schema/dic/services/services-1.0.xsd">
778+
779+
<services>
780+
<!-- Default configuration for services in *this* file -->
781+
<defaults autowire="true" autoconfigure="true" public="false" />
782+
</services>
783+
</container>
784+
785+
What does this mean? When a service is **not** public, you cannot access it directly
786+
from the container::
787+
788+
use AppBundle\Service\MessageGenerator;
789+
790+
public function newAction(MessageGenerator $messageGenerator)
791+
{
792+
// type-hinting it as an argument DOES work
793+
794+
// but accessing it directly from the container does NOT Work
795+
$this->container->get(MessageGenerator::class);
796+
}
797+
798+
Usually, this is ok: there are better ways to access a service. But, if you *do*
799+
need to make your service public, just override this setting:
800+
801+
.. configuration-block::
802+
803+
.. code-block:: yaml
804+
805+
# app/config/services.yml
806+
services:
807+
# ... same code as before
808+
809+
# explicitly configure the service
810+
AppBundle\Service\MessageGenerator:
811+
public: true
812+
813+
.. code-block:: xml
814+
815+
<!-- app/config/services.xml -->
816+
<?xml version="1.0" encoding="UTF-8" ?>
817+
<container xmlns="http://symfony.com/schema/dic/services"
818+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
819+
xsi:schemaLocation="http://symfony.com/schema/dic/services
820+
http://symfony.com/schema/dic/services/services-1.0.xsd">
821+
822+
<services>
823+
<!-- ... same code as before -->
824+
825+
<!-- Explicitly configure the service -->
826+
<service id="AppBundle\Service\MessageGenerator" public="true"></service>
827+
</services>
828+
</container>
829+
751830
Learn more
752831
----------
753832

0 commit comments

Comments
 (0)