The Bundle System

Caution

In Symfony versions prior to 4.0, it was recommended to organize your ownapplication code using bundles. This is no longer recommended and bundlesshould only be used to share code and features between multiple applications.

A bundle is similar to a plugin in other software, but even better. The corefeatures of Symfony framework are implemented with bundles (FrameworkBundle,SecurityBundle, DebugBundle, etc.) They are also used to add new features inyour application via third-party bundles.

Bundles used in your applications must be enabled perenvironment in the config/bundles.phpfile:

  1. // config/bundles.php
  2. return [
  3. // 'all' means that the bundle is enabled for any Symfony environment
  4. Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
  5. Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
  6. Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
  7. Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
  8. Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle::class => ['all' => true],
  9. Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
  10. Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle::class => ['all' => true],
  11. // this bundle is enabled only in 'dev' and 'test', so you can't use it in 'prod'
  12. Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
  13. ];

Tip

In a default Symfony application that uses Symfony Flex,bundles are enabled/disabled automatically for you when installing/removingthem, so you don't need to look at or edit this bundles.php file.

Creating a Bundle

This section creates and enables a new bundle to show there are only a few steps required.The new bundle is called AcmeTestBundle, where the Acme portion is just adummy name that should be replaced by some "vendor" name that represents you oryour organization (e.g. ABCTestBundle for some company named ABC).

Start by creating a src/Acme/TestBundle/ directory and adding a new filecalled AcmeTestBundle.php:

  1. // src/Acme/TestBundle/AcmeTestBundle.php
  2. namespace App\Acme\TestBundle;
  3.  
  4. use Symfony\Component\HttpKernel\Bundle\Bundle;
  5.  
  6. class AcmeTestBundle extends Bundle
  7. {
  8. }

Tip

The name AcmeTestBundle follows the standardBundle naming conventions. You couldalso choose to shorten the name of the bundle to simply TestBundle by namingthis class TestBundle (and naming the file TestBundle.php).

This empty class is the only piece you need to create the new bundle. Thoughcommonly empty, this class is powerful and can be used to customize the behaviorof the bundle. Now that you've created the bundle, enable it:

  1. // config/bundles.php
  2. return [
  3. // ...
  4. App\Acme\TestBundle\AcmeTestBundle::class => ['all' => true],
  5. ];

And while it doesn't do anything yet, AcmeTestBundle is now ready to be used.

Bundle Directory Structure

The directory structure of a bundle is meant to help to keep code consistentbetween all Symfony bundles. It follows a set of conventions, but is flexibleto be adjusted if needed. Take a look at AcmeDemoBundle, as it contains someof the most common elements of a bundle:

  • Controller/
  • Contains the controllers of the bundle (e.g. RandomController.php).
  • DependencyInjection/
  • Holds certain Dependency Injection Extension classes, which may import serviceconfiguration, register compiler passes or more (this directory is notnecessary).
  • Resources/config/
  • Houses configuration, including routing configuration (e.g. routing.yaml).
  • Resources/views/
  • Holds templates organized by controller name (e.g. Random/index.html.twig).
  • Resources/public/
  • Contains web assets (images, stylesheets, etc) and is copied or symbolicallylinked into the project public/ directory via the assets:install consolecommand.
  • Tests/
  • Holds all tests for the bundle.A bundle can be as small or large as the feature it implements. It containsonly the files you need and nothing else.

As you move through the guides, you'll learn how to persist objects to adatabase, create and validate forms, create translations for your application,write tests and much more. Each of these has their own place and role withinthe bundle.

Learn more