Symfony – Bundles

Symfony - Bundles

This topic is aboout Symfony – Bundles.

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, AdglobDemoBundle 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 Adglob\DemoBundle.

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

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

class AdglobDemoBundle 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 Adglob\DemoBundle\ADglobDemoBundle(), 
   ); 
   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 = Adglob/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 [Adglob/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 AdglobDemoBundle.  

Bundle name [AdDemoBundle]:  
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/Adglob/DemoBundle 
   created ./app/../src/Adglob/DemoBundle/ 
   created ./app/../src/Adglob/DemoBundle/AdglobDemoBundle.php 
   created ./app/../src/Adglob/DemoBundle/Controller/ 
   created ./app/../src/Adgllob/DemoBundle/Controller/DefaultController.php 
   created ./app/../tests/AdglobDemoBundle/Controller/   
   created ./app/../tests/AdglobDemoBundle/Controller/DefaultControllerTest.php 
   created ./app/../src/Adglob/DemoBundle/Resources/views/Default/   
   created ./app/../src/Adglob/DemoBundle/Resources/views/Default/index.html.twig 
   created ./app/../src/Adglob/DemoBundle/Resources/config/ 
   created ./app/../src/Adglob/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 :).  

In this topic we learned about Symfony – Bundles. To know more, Click Here.

Leave a Reply