User Tools

Site Tools


symfony:symfony_tutorial_notes:the_bundle_system

The Bundle System

Everything is a bundle.

  • src/AppBundle/ is your app.
  • vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/ is the framework itself.
  • In reality, a bundle can live anywhere as long as it can be autoloaded (via app/autoload.php).

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:

  • $ php app/console generate:bundle --namespace=Acme/TestBundle

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:

  • Controller/: Contains the controllers of the bundle (e.g. RandomController.php).
  • DependencyInjection/: Holds certain Dependency Injection Extension classes, which may import service configuration, register compiler passes or more (this directory is not necessary).
  • Resources/config/: Houses configuration, including routing configuration (e.g. routing.yml).
  • Resources/views/: Holds templates organized by controller name (e.g. Hello/index.html.twig).
  • Resources/public/: Contains web assets (images, stylesheets, etc) and is copied or symbolically linked into the project web/ directory via the assets:install console command.
  • Tests/: Holds all tests for the bundle.

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
symfony/symfony_tutorial_notes/the_bundle_system.txt · Last modified: 2015/09/18 04:04 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki