====== The Bundle System ====== **Everything** is a bundle. * ''src/AppBundle/'' is your app. * ''vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/'' is the framework itself. * [[http://knpbundles.com/|Third party bundles]] * 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()'': 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: - create a ''src/Acme/TestBundle/'' directory and add ''AcmeTestBundle.php'': namespace Acme\TestBundle; use Symfony\Component\HttpKernel\Bundle\Bundle; class AcmeTestBundle extends Bundle { } - enable it via the AppKernel class: public function registerBundles() { $bundles = array( // ... // register your bundle new Acme\TestBundle\AcmeTestBundle(), ); // ... return $bundles; } - 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 [[http://symfony.com/doc/current/cookbook/bundles/best_practices.html#bundles-naming-conventions|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 [[http://symfony.com/doc/current/best_practices/index.html|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