Skip to content

Commit 7cb9c23

Browse files
committed
Merge pull request #3236 from flip111/patch-2
Improvements to registering an extension (#2)
2 parents 64d8026 + 60ab4d0 commit 7cb9c23

File tree

1 file changed

+45
-41
lines changed

1 file changed

+45
-41
lines changed

cookbook/bundles/extension.rst

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,51 @@ 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
184+
following these simple conventions:
185+
186+
* The extension must be stored in the ``DependencyInjection`` sub-namespace;
187+
188+
* The extension must be named after the bundle name and suffixed with
189+
``Extension`` (``AcmeHelloExtension`` for ``AcmeHelloBundle``);
190+
191+
* The extension should provide an XSD schema.
192+
193+
Manually Registering an Extension Class
194+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
195+
196+
When not following the conventions you will have to manually register your
197+
extension. To manually register an extension class override the
198+
:method:`Bundle::build() <Symfony\\Component\\HttpKernel\\Bundle\\Bundle::build>`
199+
method in your bundle::
200+
201+
// ...
202+
use Acme\HelloBundle\DependencyInjection\UnconventionalExtensionClass;
203+
204+
class AcmeHelloBundle extends Bundle
205+
{
206+
public function build(ContainerBuilder $container)
207+
{
208+
parent::build($container);
209+
210+
// register extensions that do not follow the conventions manually
211+
$container->registerExtension(new UnconventionalExtensionClass());
212+
}
213+
}
214+
215+
In this case, the extension class must also implement a ``getAlias()`` method
216+
and return a unique alias named after the bundle (e.g. ``acme_hello``). This
217+
is required because the class name doesn't follow the conventions by ending
218+
in ``Extension``.
219+
220+
Additionally, the ``load()`` method of your extension will *only* be called
221+
if the user specifies the ``acme_hello`` alias in at least one configuration
222+
file. Once again, this is because the Extension class doesn't follow the
223+
conventions set out above, so nothing happens automatically.
224+
180225
Parsing the ``$configs`` Array
181226
------------------------------
182227

@@ -561,46 +606,5 @@ command.
561606
.. index::
562607
pair: Convention; Configuration
563608

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-
605609
.. _`FrameworkBundle Configuration`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
606610
.. _`TwigBundle Configuration`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php

0 commit comments

Comments
 (0)