@@ -11,7 +11,7 @@ you'll see a number of different configuration "namespaces", such as ``framework
11
11
you to configure things at a high level and then let the bundle make all the
12
12
low-level, complex changes that result.
13
13
14
- For example, the following tells the `` FrameworkBundle `` to enable the form
14
+ For example, the following tells the FrameworkBundle to enable the form
15
15
integration, which involves the defining of quite a few services as well
16
16
as integration of other related components:
17
17
@@ -62,15 +62,15 @@ When you create a bundle, you have two choices on how to handle configuration:
62
62
The second option - which you'll learn about in this article - is much more
63
63
flexible, but also requires more time to setup. If you're wondering which
64
64
method you should use, it's probably a good idea to start with method #1,
65
- and then change to #2 later if you need to. If you plan to distribute your
65
+ and then change to #2 later if you need to. If you plan to distribute your
66
66
bundle, the second option is recommended.
67
67
68
68
The second method has several specific advantages:
69
69
70
70
* Much more powerful than simply defining parameters: a specific option value
71
71
might trigger the creation of many service definitions;
72
72
73
- * Ability to have configuration hierarchy
73
+ * Ability to have configuration hierarchy;
74
74
75
75
* Smart merging when several configuration files (e.g. ``config_dev.yml ``
76
76
and ``config.yml ``) override each other's configuration;
@@ -89,9 +89,14 @@ The second method has several specific advantages:
89
89
supported configuration settings for which backward compatibility will
90
90
be maintained.
91
91
92
+ .. seealso ::
93
+
94
+ For parameter handling within a Dependency Injection class see
95
+ :doc: `/cookbook/configuration/using_parameters_in_dic `.
96
+
92
97
.. index ::
93
98
single: Bundle; Extension
94
- single: Dependency Injection ; Extension
99
+ single: DependencyInjection ; Extension
95
100
96
101
Creating an Extension Class
97
102
---------------------------
@@ -177,6 +182,52 @@ You can begin specifying configuration under this namespace immediately:
177
182
array. You can still provide some sensible defaults for your bundle if
178
183
you want.
179
184
185
+ Registering the Extension Class
186
+ -------------------------------
187
+
188
+ An Extension class will automatically be registered by Symfony2 when
189
+ following these simple conventions:
190
+
191
+ * The extension must be stored in the ``DependencyInjection `` sub-namespace;
192
+
193
+ * The extension must be named after the bundle name and suffixed with
194
+ ``Extension `` (``AcmeHelloExtension `` for ``AcmeHelloBundle ``);
195
+
196
+ * The extension *should * provide an XSD schema (but will be registered automatically
197
+ regardless).
198
+
199
+ Manually Registering an Extension Class
200
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
201
+
202
+ When not following the conventions, you will have to manually register your
203
+ extension. To manually register an extension class override the
204
+ :method: `Bundle::build() <Symfony\\ Component\\ HttpKernel\\ Bundle\\ Bundle::build> `
205
+ method in your bundle::
206
+
207
+ // ...
208
+ use Acme\HelloBundle\DependencyInjection\UnconventionalExtensionClass;
209
+
210
+ class AcmeHelloBundle extends Bundle
211
+ {
212
+ public function build(ContainerBuilder $container)
213
+ {
214
+ parent::build($container);
215
+
216
+ // register extensions that do not follow the conventions manually
217
+ $container->registerExtension(new UnconventionalExtensionClass());
218
+ }
219
+ }
220
+
221
+ In this case, the extension class must also implement a ``getAlias() `` method
222
+ and return a unique alias named after the bundle (e.g. ``acme_hello ``). This
223
+ is required because the class name doesn't follow the conventions by ending
224
+ in ``Extension ``.
225
+
226
+ Additionally, the ``load() `` method of your extension will *only * be called
227
+ if the user specifies the ``acme_hello `` alias in at least one configuration
228
+ file. Once again, this is because the Extension class doesn't follow the
229
+ conventions set out above, so nothing happens automatically.
230
+
180
231
Parsing the ``$configs `` Array
181
232
------------------------------
182
233
@@ -494,7 +545,7 @@ configuration arrays together.
494
545
The ``Configuration `` class can be much more complicated than shown here,
495
546
supporting array nodes, "prototype" nodes, advanced validation, XML-specific
496
547
normalization and advanced merging. You can read more about this in
497
- :doc: `the Config Component documentation </components/config/definition >`.
548
+ :doc: `the Config component documentation </components/config/definition >`.
498
549
You can also see it in action by checking out some of the core Configuration classes,
499
550
such as the one from the `FrameworkBundle Configuration `_ or the `TwigBundle Configuration `_.
500
551
@@ -504,19 +555,19 @@ Modifying the configuration of another Bundle
504
555
If you have multiple bundles that depend on each other, it may be useful
505
556
to allow one ``Extension `` class to modify the configuration passed to another
506
557
bundle's ``Extension `` class, as if the end-developer has actually placed that
507
- configuration in his/her ``app/config/config.yml `` file.
558
+ configuration in their ``app/config/config.yml `` file.
508
559
509
560
For more details, see :doc: `/cookbook/bundles/prepend_extension `.
510
561
511
562
Default Configuration Dump
512
563
~~~~~~~~~~~~~~~~~~~~~~~~~~
513
564
514
565
The ``config:dump-reference `` command allows a bundle's default configuration to
515
- be output to the console in yaml .
566
+ be output to the console in YAML .
516
567
517
568
As long as your bundle's configuration is located in the standard location
518
569
(``YourBundle\DependencyInjection\Configuration ``) and does not have a
519
- ``__construct() `` it will work automatically. If you have something
570
+ ``__construct() `` it will work automatically. If you have something
520
571
different, your ``Extension `` class must override the
521
572
:method: `Extension::getConfiguration() <Symfony\\ Component\\ HttpKernel\\ DependencyInjection\\ Extension::getConfiguration> `
522
573
method and return an instance of your
@@ -552,52 +603,11 @@ Comments and examples can be added to your configuration nodes using the
552
603
}
553
604
}
554
605
555
- This text appears as yaml comments in the output of the ``config:dump-reference ``
606
+ This text appears as YAML comments in the output of the ``config:dump-reference ``
556
607
command.
557
608
558
609
.. index ::
559
610
pair: Convention; Configuration
560
611
561
- Extension Conventions
562
- ---------------------
563
-
564
- When creating an extension, follow these simple conventions:
565
-
566
- * The extension must be stored in the ``DependencyInjection `` sub-namespace;
567
-
568
- * The extension must be named after the bundle name and suffixed with
569
- ``Extension `` (``AcmeHelloExtension `` for ``AcmeHelloBundle ``);
570
-
571
- * The extension should provide an XSD schema.
572
-
573
- If you follow these simple conventions, your extensions will be registered
574
- automatically by Symfony2. If not, override the
575
- :method: `Bundle::build() <Symfony\\ Component\\ HttpKernel\\ Bundle\\ Bundle::build> `
576
- method in your bundle::
577
-
578
- // ...
579
- use Acme\HelloBundle\DependencyInjection\UnconventionalExtensionClass;
580
-
581
- class AcmeHelloBundle extends Bundle
582
- {
583
- public function build(ContainerBuilder $container)
584
- {
585
- parent::build($container);
586
-
587
- // register extensions that do not follow the conventions manually
588
- $container->registerExtension(new UnconventionalExtensionClass());
589
- }
590
- }
591
-
592
- In this case, the extension class must also implement a ``getAlias() `` method
593
- and return a unique alias named after the bundle (e.g. ``acme_hello ``). This
594
- is required because the class name doesn't follow the standards by ending
595
- in ``Extension ``.
596
-
597
- Additionally, the ``load() `` method of your extension will *only * be called
598
- if the user specifies the ``acme_hello `` alias in at least one configuration
599
- file. Once again, this is because the Extension class doesn't follow the
600
- standards set out above, so nothing happens automatically.
601
-
602
612
.. _`FrameworkBundle Configuration` : https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
603
613
.. _`TwigBundle Configuration` : https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php
0 commit comments