Skip to content

Commit 69e76ab

Browse files
committed
Improvements to registering an extension (#2)
| Q | A | ------------- | --- | Doc fix? | yes | New docs? | no | Applies to | all | Fixed tickets | #9644 (symfony) and #3231 (symfony-docs) 1. The original text is good but the topic of that paragraph is not "extension conventions" but "registering an extension" (i.e. if you follow these conventions then register goes like so ..) 2. Cleaned up different terminology "Conventions" v "Standards" -> just conventions now 3. Move this part to a more logical place. First create then register, then modify for additional needs.
1 parent 2e649b3 commit 69e76ab

File tree

1 file changed

+43
-44
lines changed

1 file changed

+43
-44
lines changed

cookbook/bundles/extension.rst

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,49 @@ You can begin specifying configuration under this namespace immediately:
177177
array. You can still provide some sensible defaults for your bundle if
178178
you want.
179179

180+
Registering the Extension class
181+
-------------------------------
182+
183+
An Extension class will automatically be registered by Symfony2 when following these simple conventions:
184+
185+
* The extension must be stored in the ``DependencyInjection`` sub-namespace;
186+
187+
* The extension must be named after the bundle name and suffixed with
188+
``Extension`` (``AcmeHelloExtension`` for ``AcmeHelloBundle``);
189+
190+
* The extension should provide an XSD schema.
191+
192+
Manually registering an Extension class
193+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
194+
195+
To manually register an extension class override the
196+
:method:`Bundle::build() <Symfony\\Component\\HttpKernel\\Bundle\\Bundle::build>`
197+
method in your bundle::
198+
199+
// ...
200+
use Acme\HelloBundle\DependencyInjection\UnconventionalExtensionClass;
201+
202+
class AcmeHelloBundle extends Bundle
203+
{
204+
public function build(ContainerBuilder $container)
205+
{
206+
parent::build($container);
207+
208+
// register extensions that do not follow the conventions manually
209+
$container->registerExtension(new UnconventionalExtensionClass());
210+
}
211+
}
212+
213+
In this case, the extension class must also implement a ``getAlias()`` method
214+
and return a unique alias named after the bundle (e.g. ``acme_hello``). This
215+
is required because the class name doesn't follow the conventions by ending
216+
in ``Extension``.
217+
218+
Additionally, the ``load()`` method of your extension will *only* be called
219+
if the user specifies the ``acme_hello`` alias in at least one configuration
220+
file. Once again, this is because the Extension class doesn't follow the
221+
conventions set out above, so nothing happens automatically.
222+
180223
Parsing the ``$configs`` Array
181224
------------------------------
182225

@@ -511,9 +554,6 @@ For more details, see :doc:`/cookbook/bundles/prepend_extension`.
511554
Default Configuration Dump
512555
~~~~~~~~~~~~~~~~~~~~~~~~~~
513556

514-
.. versionadded:: 2.1
515-
The ``config:dump-reference`` command was added in Symfony 2.1
516-
517557
The ``config:dump-reference`` command allows a bundle's default configuration to
518558
be output to the console in YAML.
519559

@@ -561,46 +601,5 @@ command.
561601
.. index::
562602
pair: Convention; Configuration
563603

564-
Extension Conventions
565-
---------------------
566-
567-
When creating an extension, follow these simple conventions:
568-
569-
* The extension must be stored in the ``DependencyInjection`` sub-namespace;
570-
571-
* The extension must be named after the bundle name and suffixed with
572-
``Extension`` (``AcmeHelloExtension`` for ``AcmeHelloBundle``);
573-
574-
* The extension should provide an XSD schema.
575-
576-
If you follow these simple conventions, your extensions will be registered
577-
automatically by Symfony2. If not, override the
578-
:method:`Bundle::build() <Symfony\\Component\\HttpKernel\\Bundle\\Bundle::build>`
579-
method in your bundle::
580-
581-
// ...
582-
use Acme\HelloBundle\DependencyInjection\UnconventionalExtensionClass;
583-
584-
class AcmeHelloBundle extends Bundle
585-
{
586-
public function build(ContainerBuilder $container)
587-
{
588-
parent::build($container);
589-
590-
// register extensions that do not follow the conventions manually
591-
$container->registerExtension(new UnconventionalExtensionClass());
592-
}
593-
}
594-
595-
In this case, the extension class must also implement a ``getAlias()`` method
596-
and return a unique alias named after the bundle (e.g. ``acme_hello``). This
597-
is required because the class name doesn't follow the standards by ending
598-
in ``Extension``.
599-
600-
Additionally, the ``load()`` method of your extension will *only* be called
601-
if the user specifies the ``acme_hello`` alias in at least one configuration
602-
file. Once again, this is because the Extension class doesn't follow the
603-
standards set out above, so nothing happens automatically.
604-
605604
.. _`FrameworkBundle Configuration`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
606605
.. _`TwigBundle Configuration`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php

0 commit comments

Comments
 (0)