8000 Updates to DI config for 3.3 by weaverryan · Pull Request #7807 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

Updates to DI config for 3.3 #7807

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 23 commits into from
May 5, 2017
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8433fc1
[WIP] Updates to DI config for 3.3
weaverryan Apr 15, 2017
2d11347
more tweaks
weaverryan Apr 28, 2017
105801c
adding note about autoconfigure
weaverryan Apr 28, 2017
049df7d
Adding details and usages of fetching the service as a controller arg
weaverryan Apr 28, 2017
9e84572
last tweaks from feedback
weaverryan Apr 28, 2017
c45daf4
fixing build problem
weaverryan Apr 28, 2017
2636bea
bad link
weaverryan Apr 28, 2017
9ab27f0
Add xml files
GuilhemN Apr 28, 2017
0e48bd8
[WIP] Updates to DI config for 3.3
weaverryan Apr 15, 2017
6e6ed94
more tweaks
weaverryan Apr 28, 2017
70178d1
adding note about autoconfigure
weaverryan Apr 28, 2017
45500b3
Adding details and usages of fetching the service as a controller arg
weaverryan Apr 28, 2017
759e9b2
last tweaks from feedback
weaverryan Apr 28, 2017
6de83e2
fixing build problem
weaverryan Apr 28, 2017
89e12de
bad link
weaverryan Apr 28, 2017
443aec2
Merge pull request #7857 from GuilhemN/patch-1
weaverryan May 2, 2017
bc7088d
Merge remote-tracking branch 'origin/di-3.3-changes' into di-3.3-changes
weaverryan May 2, 2017
ee27765
Adding versionadded
weaverryan May 2, 2017
5452c61
Adding section about public: false
weaverryan May 2, 2017
2229fd3
Merge remote-tracking branch 'origin/master' into di-3.3-changes
weaverryan May 2, 2017
cac3c6c
Merge remote-tracking branch 'origin/master' into di-3.3-changes
weaverryan May 5, 2017
12c4944
Tweaks after amazing review from @GuilhemN and @xabbuh
weaverryan May 5, 2017
22adfbd
removing duplicate target
weaverryan May 5, 2017
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
Prev Previous commit
Next Next commit
Adding section about public: false
  • Loading branch information
weaverryan committ 8000 ed May 2, 2017
commit 5452c617d2686463354d1a0491ca2e69bf1da9c2
79 changes: 79 additions & 0 deletions service_container.rst
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,85 @@ it as a service. Then ``autoconfigure`` will add the ``twig.extension`` tag *for
you, because your class implements ``Twig_ExtensionInterface``. And thanks to ``autowire``,
you can even add constructor arguments without any configuration.

.. _container-public:

Public Versus Private Services
------------------------------

Thanks to the ``_defaults`` section in ``services.yml``, every service defined in
this file is ``public: false`` by default:

.. configuration-block::

.. code-block:: yaml

# app/config/services.yml
services:
# default configuration for services in *this* file
_defaults:
# ...
public: false

.. code-block:: xml

<!-- app/config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<!-- Default configuration for services in *this* file -->
<defaults autowire="true" autoconfigure="true" public="false" />
</services>
</container>

What does this mean? When a service is **not** public, you cannot access it directly
from the container::

use AppBundle\Service\MessageGenerator;

public function newAction(MessageGenerator $messageGenerator)
{
// type-hinting it as an argument DOES work

// but accessing it directly from the container does NOT Work
$this->container->get(MessageGenerator::class);
}

Usually, this is ok: there are better ways to access a service. But, if you *do*
need to make your service public, just override this setting:

.. configuration-block::

.. code-block:: yaml

# app/config/services.yml
services:
# ... same code as before

# explicitly configure the service
AppBundle\Service\MessageGenerator:
public: true

.. code-block:: xml

<!-- app/config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<!-- ... same code as before -->

<!-- Explicitly configure the service -->
<service id="AppBundle\Service\MessageGenerator" public="true"></service>
</services>
</container>

Learn more
----------

Expand Down
0