Table of Contents

The Bundle System

Everything is a bundle.

Enabling bundles

Enabling bundles is done by hacking AppKernel.registerBundles():

app/AppKernel.php
public function registerBundles()
{
    $bundles = array(
        new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
        new Symfony\Bundle\SecurityBundle\SecurityBundle(),
        new Symfony\Bundle\TwigBundle\TwigBundle(),
        new Symfony\Bundle\MonologBundle\MonologBundle(),
        new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
        new Symfony\Bundle\DoctrineBundle\DoctrineBundle(),
        new Symfony\Bundle\AsseticBundle\AsseticBundle(),
        new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
        new AppBundle\AppBundle(),
    );
 
    if (in_array($this->getEnvironment(), array('dev', 'test'))) {
        $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
        $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
        $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
    }
 
    return $bundles;
}

Creating a Bundle

To create AcmeTestBundle manually:

  1. create a src/Acme/TestBundle/ directory and add AcmeTestBundle.php:
    src/Acme/TestBundle/AcmeTestBundle.php
    namespace Acme\TestBundle;
     
    use Symfony\Component\HttpKernel\Bundle\Bundle;
     
    class AcmeTestBundle extends Bundle
    {
    }
  2. enable it via the AppKernel class:
    app/AppKernel.php
    public function registerBundles()
    {
        $bundles = array(
            // ...
            // register your bundle
            new Acme\TestBundle\AcmeTestBundle(),
        );
        // ...
     
        return $bundles;
    }
  3. Hack on controllers, etc.

To create AcmeTestBundle via the command line:

The bundle skeleton generates a basic controller, template, and routing resource.

The generate:bundle command adds the new bundle to AppKernel.registerBundles().

See Bundle Naming Conventions.

Bundle Directory Structure

By default, the bundle system follows a set of conventions that help to keep code consistent:

Note that the current best practices for the AppBundle are:

  • config goes in app/config/ (rather than src/AppBundle/Resources/config/)
  • controllers go in src/AppBundle/Controller/ (as expected).
  • views go in app/Resources/views/ (rather than src/AppBundle/Resources/views/)
  • assets go in web/
  • tests go in TBD