Monday, August 23, 2010


In this application we are using latest symfony2 code. There are so many different changes are made in this latest version. You can get the original symfony2 code from

So we have to make a custom application. Sugarbox can be downloaded from here.

The steps:

  1. Download sugarbox from above mentioned URL
  2. unzip and upload to server root

This the final structure.
symfony2 basic:

web/index.php is front controller. It's the only php file that access from a browser. So all your php files are well protected. Index.php accepts the HTTP request from the browser and sends it to the application. S2 is kernal based application. Kernal is the heart of all s2 operations. So to create a s2 app we need to create sub class of this kernal (abstract) class. This is placed under blog folder. So create a php file called BlogKernel.php. The naming is very important. Create another folder 'config' inside blog main folder. All configuration files and routing files are placed under config folder. For this tutorial we not go for caching and logging mechanism. We can back here after finishing this tutorial.

In s2, all are in the form of bundles. 'Bundles are first class citizen'. So our application bundle(BlogBundle) will place inside Application folder. Ok. So let's do some coding.

Front Controller
Open web/index.php file using your favourite text editor. 

 require_once __DIR__.'/../blog/BlogKernel.php';

$kernel = new BlogKernel('dev', true);

These are the only lines that goes into index.php. Here we create a new instance of our Blogkernal class which we will create soon. In $kernel = new BlogKernel('prod', false); line, 'prod' and 'false' are two arguments that pass to the blog application. 'prod' means setup the production environment and false means no debugging. Instead of 'prod' you can give 'dev' for development. In development environment you need debugging on – so instead of 'false' we write 'true'. 

Application kernel
Ok lets move to BlogKernel.php.
require_once __DIR__.'/../src/autoload.php';

use Symfony\Framework\Kernel;
use Symfony\Component\DependencyInjection\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class BlogKernel extends Kernel
    public function registerRootDir()
        return __DIR__;

    public function boot()

        return parent::boot();

    public function registerBundles()
        $bundles = array(
            new Symfony\Framework\KernelBundle(),
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\ZendBundle\ZendBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Symfony\Bundle\DoctrineBundle\DoctrineBundle(),
            //new Symfony\Bundle\DoctrineMigrationsBundle\DoctrineMigrationsBundle(),
            //new Symfony\Bundle\DoctrineMongoDBBundle\DoctrineMongoDBBundle(),
            //new Symfony\Bundle\PropelBundle\PropelBundle(),
            //new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Application\BlogBundle\BlogBundle(),

        if ($this->isDebug()) {

        return $bundles;

    public function registerBundleDirs()
        return array(
            'Application'     => __DIR__.'/../src/Application',
            'Bundle'          => __DIR__.'/../src/Bundle',
            'Symfony\\Bundle' => __DIR__.'/../src/vendor/symfony/src/Symfony/Bundle',
//to load config files
    public function registerContainerConfiguration(LoaderInterface $loader)
        // use YAML for configuration
        // comment to use another configuration format
        $container = new ContainerBuilder();
        $container->setParameter('validator.message_interpolator.class', 'Application\\BlogBundle\\Validator\\NoValidationXliffMessageInterpolator');
        return $container;

        // uncomment to use XML for configuration

        // uncomment to use PHP for configuration

i think the above is self explanatory.
Now we go to the autoload.php. 


  1. what kind article is this... just code!! ... please spent some more time in writing !!

  2. Hi,
    I'm currently learning symfony 2 for my personal blog, and I'm confronted to a limitation. I've created my own validator, but I need to inject the container on it. Do you have an idea how I can do that ? thanks ;)