Symfony - Bundles


Advertisements

A Symfony bundle is a collection of files and folders organized in a specific structure. The bundles are modeled in such a way that it can be reused in multiple applications. The main application itself is packaged as a bundle and it is generally called AppBundle.

A bundle may be packaged specific to an application such as AdminBundle (admin section), BlogBundle (site's blog), etc. Such bundles cannot be shared between an application. Instead, we can model a certain part of the application such as blogs as generic bundle so that we can simply copy the bundle from one application to another application to reuse the blog functionality.

Structure of a Bundle

The basic structure of a bundle is as follows.

  • Controller − All controller need to be placed here.

  • DependencyInjection − All dependency injection related code and configuration need to be placed here.

  • Resources/config − Bundle related configurations are placed here.

  • Resources/view − Bundle related view templates are placed here.

  • Resources/public − Bundle related stylesheets, JavaScripts, images, etc., are placed here.

  • Tests − Bundle related unit test files are placed here.

Creating a Bundle

Let us create a simple bundle, HowcodexDemoBundle in our HelloWorld application.

Step 1 − Choose a namespace. Namespace of a bundle should include vendor name and bundle name. In our case, it is Howcodex\DemoBundle.

Step 2 − Create an empty class, HowcodexDemoBundle by extending Bundle class and place it under src/Howcodex/DemoBundle.

namespace Howcodex\DemoBundle; 
use Symfony\Component\HttpKernel\Bundle\Bundle;  

class HowcodexDemoBundle extends Bundle { 
}

Step 3 − Register the class in the list of bundle supported by the application in AppKernel class.

public function registerBundles() { 
   $bundles = array( 
      // ... 
      // register your bundle 
      new Howcodex\DemoBundle\HowcodexDemoBundle(), 
   ); 
   return $bundles; 
}

This is all is needed to create an empty bundle and all other concepts are the same as that of the application. Symfony also provides a console command generate:bundle to simplify the process of creating a new bundle, which is as follows.

php bin/console generate:bundle --namespace = Howcodex/DemoBundle

Result

Welcome to the Symfony bundle generator!

Are you planning on sharing this bundle across multiple applications? [no]: no  

Your application code must be written in bundles. This command helps 
you generate them easily.  

Give your bundle a descriptive name, like BlogBundle. 
Bundle name [Howcodex/DemoBundle]:   

In your code, a bundle is often referenced by its name. It can be the 
concatenation of all namespace parts but it's really up to you to come 
up with a unique name (a good practice is to start with the vendor name). 
Based on the namespace, we suggest HowcodexDemoBundle.  

Bundle name [HowcodexDemoBundle]:  
Bundles are usually generated into the src/ directory. Unless you're 
doing something custom, hit enter to keep this default!
Target Directory [src/]:   

What format do you want to use for your generated configuration?  

Configuration format (annotation, yml, xml, php) [annotation]:

Bundle generation

> Generating a sample bundle skeleton into app/../src/Howcodex/DemoBundle 
   created ./app/../src/Howcodex/DemoBundle/ 
   created ./app/../src/Howcodex/DemoBundle/HowcodexDemoBundle.php 
   created ./app/../src/Howcodex/DemoBundle/Controller/ 
   created ./app/../src/Howcodex/DemoBundle/Controller/DefaultController.php 
   created ./app/../tests/HowcodexDemoBundle/Controller/   
   created ./app/../tests/HowcodexDemoBundle/Controller/DefaultControllerTest.php 
   created ./app/../src/Howcodex/DemoBundle/Resources/views/Default/   
   created ./app/../src/Howcodex/DemoBundle/Resources/views/Default/index.html.twig 
   created ./app/../src/Howcodex/DemoBundle/Resources/config/ 
   created ./app/../src/Howcodex/DemoBundle/Resources/config/services.yml 
> Checking that the bundle is autoloaded 
> Enabling the bundle inside app/AppKernel.php 
   updated ./app/AppKernel.php 
> Importing the bundle's routes from the app/config/routing.yml file 
   updated ./app/config/routing.yml 
> Importing the bundle's services.yml from the app/config/config.yml file 
   updated ./app/config/config.yml 
Everything is OK! Now get to work :).  
Advertisements