How do I get a list of bundles in symfony2/symfony3?

23,652

Solution 1

The easiest way to do it in console and that outputs bundle names correctly is:

Symfony 2

php app/console config:dump-reference

Symfony 3

php bin/console config:dump-reference

The key here is not to provide any options or arguments. In this case, the command simply outputs all available bundles:

Available registered bundles with their extension alias if available:
+------------------------------------+-----------------------------------+
| Bundle name                        | Extension alias                   |
+------------------------------------+-----------------------------------+
| FrameworkBundle                    | framework                         |
| SecurityBundle                     | security                          |
| TwigBundle                         | twig                              |
| MonologBundle                      | monolog                           |
| SwiftmailerBundle                  | swiftmailer                       |
| DoctrineBundle                     | doctrine                          |
| AsseticBundle                      | assetic                           |
| GearmanBundle                      | gearman                           |
| SMMemcacheBundle                   | sm_memcache                       |
| PrestaSitemapBundle                | presta_sitemap                    |
| DoctrineCacheBundle                | doctrine_cache                    |
| CybernoxAmazonWebServicesBundle    | cybernox_amazon_web_services      |
| FOSFacebookBundle                  | fos_facebook                      |
| HWIOAuthBundle                     | hwi_oauth                         |
| FkrSimplePieBundle                 | fkr_simple_pie                    |
| RMSPushNotificationsBundle         | rms_push_notifications            |
| RobertoTruToInlineStyleEmailBundle | roberto_tru_to_inline_style_email |
| InsomniaMaxMindGeoIpBundle         | insomnia_max_mind_geo_ip          |
| EWZRecaptchaBundle                 | ewz_recaptcha                     |
| MopaBootstrapBundle                | mopa_bootstrap                    |
| JanThomas89MailSafeBundle          | jan_thomas89_mail_safe            |
| WebProfilerBundle                  | web_profiler                      |
| SensioDistributionBundle           | sensio_distribution               |
| SensioGeneratorBundle              |                                   |
+------------------------------------+-----------------------------------+

Solution 2

If you have container object available then you can get array of the enabled bundles by $this->container->getParameter('kernel.bundles');

Solution 3

  1. You can define a static function in each bundle. Ex: YourBundle::yourStaticFunction();
  2. Use $this->container->getParameter('kernel.bundles') to get the list of bundles. This only returns the bundle class names instead of the Bundle object. Go through each bundle, check if the bundle has the function yourStaticFunction(), Hint: Use method_exists(). If the method exists, then call ::yourStaticFunction();

Solution 4

In console you can use php app/console container:debug --parameter=kernel.bundles

Solution 5

If you want to call a non static method of registered bundle object (not class) then you can do the following:

$kernel = $this->container->get('kernel');
$bundles = $kernel->getBundles();
$bundles['YourBundleName']->someMethod();

Where 'YourBundleName' is the name of your bundle, which you can get by calling from console:

php app/console config:dump-reference
Share:
23,652
Mike
Author by

Mike

PHP developer, certified scrum master, team lead, photographer, blogger and all round nice guy!

Updated on July 09, 2022

Comments

  • Mike
    Mike almost 2 years

    I've just started using symfony and I'd like to get a list of bundles from a particular vendor, iterate through them and call a $bundle->renderSomething() function on each default controller.

    Firstly, I need to get the list of bundles to iterate, or iterate through each object. Any ideas on the best way to do that?

  • rhuffstedtler
    rhuffstedtler almost 10 years
    For some reason, doing that at the console truncates the output in the middle of the first bundle for me: rob@vagrant-ubuntu-trusty-64:/var/www/html/Symfony$ php app/console container:debug --parameter=kernel.bundles|more {"FrameworkBundle":"Symfony\\Bundle\\FrameworkBundle\\Framew‌​... rob@vagrant-ubuntu-trusty-64:/var/www/html/Symfony$