8000 adding note about autoconfigure · symfony/symfony-docs@105801c · GitHub
[go: up one dir, main page]

Skip to content

Commit 105801c

Browse files
committed
adding note about autoconfigure
1 parent 2d11347 commit 105801c

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

service_container.rst

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ the service container *how* to instantiate it:
123123
124124
# app/config/services.yml
125125
services:
126+
# default configuration for services in *this* file
126127
_defaults:
127128
autowire: true
128129
autoconfigure: true
@@ -510,6 +511,70 @@ service whose id is ``monolog.logger.request``.
510511
the *service* whose id is ``monolog.logger.request``, and not just the *string*
511512
``monolog.logger.request``.
512513

514+
The autoconfigure Option
515+
------------------------
516+
517+
Above, we've set ``autoconfigure: true`` in the ``_defaults`` section so that it
518+
applies to all services defined in that file. With this setting, the container will
519+
automatically apply certain configuration to your services, based on your service's
520+
*class*. The is mostly used to *auto-tag* your services.
521+
522+
For example, to create a Twig Extension, you need to create a class, register it
523+
as a service, and :doc:`tag </tags>` it with ``twig.extension``:
524+
525+
.. configuration-block::
526+
527+
.. code-block:: yaml
528+
529+
# app/config/services.yml
530+
services:
531+
# ...
532+
533+
AppBundle\Twig\MyTwigExtension:
534+
tags: [twig.extension]
535+
536+
.. code-block:: xml
537+
538+
<!-- app/config/services.xml -->
539+
TODO
540+
541+
.. code-block:: php
542+
543+
// app/config/services.php
544+
TODO
545+
546+
But, with ``autoconfigure: true``, you don't need the tag. In fact, all you need
547+
is this:
548+
549+
.. configuration-block::
550+
551+
.. code-block:: yaml
552+
553+
# app/config/services.yml
554+
services:
555+
_defaults:
556+
autowire: true
557+
autoconfigure: true
558+
559+
# load your service from the Twig directory
560+
AppBundle\:
561+
resource: '../../src/AppBundle/{Service,EventDispatcher,Twig,Form}'
562+
563+
.. code-block:: xml
564+
565+
<!-- app/config/services.xml -->
566+
TODO
567+
568+
.. code-block:: php
569+
570+
// app/config/services.php
571+
TODO
572+
573+
That's it! The container will find your class in the ``Twig/`` directory and register
574+
it as a service. Then ``autoconfigure`` will add the ``twig.extension`` tag *for*
575+
you, because your class implements ``Twig_ExtensionInterface``. And thanks to ``autowire``,
576+
you can even add ``__construct()`` arguments without any configuration.
577+
513578
Learn more
514579
----------
515580

0 commit comments

Comments
 (0)
0