-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Changes from 1 commit
8433fc1
2d11347
105801c
049df7d
9e84572
c45daf4
2636bea
9ab27f0
0e48bd8
6e6ed94
70178d1
45500b3
759e9b2
6de83e2
89e12de
443aec2
bc7088d
ee27765
5452c61
2229fd3
cac3c6c
12c4944
22adfbd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,6 +123,7 @@ the service container *how* to instantiate it: | |
|
||
# app/config/services.yml | ||
services: | ||
# default configuration for services in *this* file | ||
_defaults: | ||
autowire: true | ||
autoconfigure: true | ||
|
@@ -510,6 +511,70 @@ service whose id is ``monolog.logger.request``. | |
the *service* whose id is ``monolog.logger.request``, and not just the *string* | ||
``monolog.logger.request``. | ||
|
||
The autoconfigure Option | ||
------------------------ | ||
|
||
Above, we've set ``autoconfigure: true`` in the ``_defaults`` section so that it | ||
applies to all services defined in that file. With this setting, the container will | ||
automatically apply certain configuration to your services, based on your service's | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
*class*. The is mostly used to *auto-tag* your services. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
For example, to create a Twig Extension, you need to create a class, register it | ||
as a service, and :doc:`tag </tags>` it with ``twig.extension``: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/services.yml | ||
services: | ||
# ... | ||
|
||
AppBundle\Twig\MyTwigExtension: | ||
tags: [twig.extension] | ||
|
||
.. code-block:: xml | ||
|
||
<!-- app/config/services.xml --> | ||
TODO | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. <?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>
<!-- ... -->
<service id="AppBundle\Twig\MyTwigExtension">
<tag name="twig.extension" />
</service>
</services>
</container> |
||
|
||
.. code-block:: php | ||
|
||
// app/config/services.php | ||
TODO | ||
|
||
But, with ``autoconfigure: true``, you don't need the tag. In fact, all you need | ||
is this: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/services.yml | ||
services: | ||
_defaults: | ||
autowire: true | ||
autoconfigure: true | ||
|
||
# load your service from the Twig directory | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
AppBundle\: | ||
resource: '../../src/AppBundle/{Service,EventDispatcher,Twig,Form}' | ||
|
||
.. code-block:: xml | ||
|
||
<!-- app/config/services.xml --> | ||
TODO | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. <?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>
<defaults autowire="true" autoconfigure="true" />
<!-- Load your services-->
<prototype namespace="AppBundle\" resource="../../src/AppBundle/{Service,EventDispatcher,Twig,Form}" />
</services>
</container> |
||
|
||
.. code-block:: php | ||
|
||
// app/config/services.php | ||
TODO | ||
|
||
That's it! The container will find your class in the ``Twig/`` directory and register | ||
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 ``__construct()`` arguments without any configuration. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
Learn more | ||
---------- | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this isn't merged yet - symfony/symfony#22234 - but has all the 👍 so I expect it will
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd encourage adding
public: false
also