Symfony 2: Creating a Bundle

As discussed in this and this post, Symfony 2 is a web application framework. In Symfony 2 we organize our code in form of bundles. You can think bundle as collection of all code. Say if your app has a front-end, a back-end and an API then you can create three different bundles one for your front-end, one for your back-end and one for API. This will allow your to manage code efficiently.

Symfony 2 is flexible and it allows you to have as many as bundles you want in your app. Let’s understand this bundle system in detail by creating a bundle.


Before starting you will need to set up Symfony 2 in your webroot directory. I have already covered this topic in an earlier post. Now first download the Symfony 2 and extract it in your webroot directory and rename the ‘symfony’ folder to ‘randomno’ (since we are creating an app which will generate a random number, but of-course you can give any name you want).

In Symfony 2, we can either manually generate and register a bundle with kernel or we can use inbuilt bundle generator to create and register our bundle. To make our development work easy, we will use this automatic bundle generator.

To use this bundle generator, you have to navigate to your Symfony 2 directory i.e. directory with bin, src folders in it (not your webroot directory) and open command window/console there.

Here in my case, I have opened console in D:/wamp/www/randomno since I have installed WAMP in ‘D’ drive, ‘www’ is my webroot directory and I have renamed Symfony folder to ‘randomno’.

Generating a bundle

Syntax for creating bundle is

php app/console generate:bundle --namespace={namespace} --format={format}

Now you have to execute following command to create a bundle named ‘randomno’.

D:\wamp\www\randomno>php app/console generate:bundle --namespace=Me/RandomnoBundle --format=yml

Here in this command, namespace specifies the bundle name which should be in {Company_Name}/{Bundle_Name}Bundle format (make sure that you end the name with ‘bundle’) while format specifies how the configuration will be specified i.e. whether in XML, YML or in PHP etc. The name space we will be using is ‘randomno’ and we will be using ‘yml’ as format.

When you hit enter you will see.

  Welcome to the Symfony2 bundle generator



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 MeRandomnoBundle.

Bundle name [MeRandomnoBundle]:

Now it is asking you bundle name i.e. namespace. As we have already specified our namespace, you have to hit enter. In case you want to change the namespace, you can specify here in this stage.

Then it will ask you for target directory. Just leave this option as it is. It is used by advance users or you can use it in complex projects.

The bundle can be generated anywhere. The suggested default directory uses
the standard conventions.

Target directory [D:/wamp/www/randomno/src]:

Now bundle generator is asking you whether you want full directory structure or not. Choice is yours! But to make our developement simple, we will go with ‘yes’ option as shown below. Just write ‘yes’ and hit enter.

To help you get started faster, the command can generate some
code snippets for you.

Do you want to generate the whole directory structure [no]? yes

It is asking for your confirmation. Just hit enter.



  Summary before generation


You are going to generate a "Me\RandomnoBundle\MeRandomnoBundle" bundle
in "D:/wamp/www/randomno/src/" using the "yml" format.

Do you confirm generation [yes]?

Automatic update of kernel means whether the bundle generator should update the kernel or not. Basically  when we generate any bundle in Symfony 2, we have to register it with app kernel and routes. Here in this case, since we want to use our newly created bundle, we will type ‘yes’ and proceed with generation. (You can register your bundle manually also)


  Bundle generation


Generating the bundle code: OK
Checking that the bundle is autoloaded: OK
Confirm automatic update of your Kernel [yes]?
Enabling the bundle inside the Kernel: OK

Now Symfony 2 is asking whether it should update the routes or not. As discussed above, to use our bundle, we need to update our routes also. Hence type ‘yes’ and proceed.

Confirm automatic update of the Routing [yes]?
Importing the bundle routing resource: OK


  You can now start using the generated code!



D:\wamp\www\randomno>

Now you can open app kernel and routes to whether our bundle is properly registered or not.

You can check AppKernel by opening ‘AppKernel.php’ file from ‘app’ folder.

...

class AppKernel extends Kernel
{
	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\AsseticBundle\AsseticBundle(),
        	new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
        	new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
        	new Me\RandomnoBundle\MeRandomnoBundle(),
    	);

 		...

    	return $bundles;
	}
...
}

You can check routing configuration by opening ‘routing.yml’ file from ‘app\config’ folder.

me_randomno:
    resource: "@MeRandomnoBundle/Resources/config/routing.yml"
    prefix:   /

You can find the newly created bundle in ‘src’ directory.

Testing our bundle

Now in order to test bundle you have to simply navigate to following urls.

http://localhost/randomno/web/app_dev.php

You will see Welcome page of Symfony 2 like this:

Symfony Welcome Page
Symfony Welcome Page

Another url you can use is:

http://localhost/randomno/web/app_dev.php/hello/ankur

This page will show hello message.

If both url are returning proper pages then all done! Else first check url. If problem still exists then you might have missed some step, go through this post once again. While creating a bundle, the bundle generator creates one default controller, one route and one view for you. Hence this urls should work without any coding.

Reference:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.