User Tools

Site Tools


symfony:symfony_tutorial_notes:the_bundle_system

This is an old revision of the document!


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

TODO

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 practice for the AppBundle seems to be that:

  • controllers go in src/AppBundle/Controller/ as expected.
  • config goes in app/config rather than src/AppBundle/Resources/config/
  • views go in app/Resources/views rather than src/AppBundle/Resources/views/
  • assets go in TBD
  • tests go in TBD
symfony/symfony_tutorial_notes/the_bundle_system.1442452526.txt.gz · Last modified: 2015/09/17 01:15 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki