Symfony2 - Setup an Application in a minute

  1. Download

    Download the Symfony Standard Edition from symfony.com

    At the moment of writing this post latest stable version is 2.1

         cd /home/user/Downloads/
         wget --content-disposition 'http://symfony.com/download?v=Symfony_Standard_Vendors_2.1.4.tgz'
         tar -zxvf Symfony_Standard_Vendors_2.1.4.tgz
         mv Symfony /var/www/symfony
    

    Then Point /var/www/symfony/web directory to the your web server

  2. Configuration

    Now We are going to use app.php as the main index file instead of playing with app_dev.php within the development environment.

         vim web/app.php
    

    Replace your app.php with the below code

         // Make group permissions stick in `cache/log` dirs
         umask(0002);
    
         use Symfony\Component\ClassLoader\ApcClassLoader;
         use Symfony\Component\HttpFoundation\Request;
    
         $loader = require_once __DIR__.'/../app/bootstrap.php.cache';
    
         require_once __DIR__.'/../app/AppKernel.php';
         $env = 'prod';
         if (isset($_SERVER['HTTP_HOST']))
         {
             if (preg_match('/(localhost|local\.)/', $_SERVER['HTTP_HOST']))
             {
                 $env = 'dev';
             }
         }
    
         $kernel = new AppKernel($env, $env == 'dev');
         $kernel->loadClassCache();
    
         $request = Request::createFromGlobals();
    
         $response = $kernel->handle($request);
         $response->send();
    
         $kernel->terminate($request, $response);
    

    Load your Symfony site by typing http://localhost/symfony in your web browser. Now you can access Symfony Development environment without visiting to app_dev.php.


Why Symfony2?

With the PHP5 there was a big hype in PHP frameworks and MVC pattern. As a result, lots of developers got lost within frameworks while trying to figure out the best. I was one of them.

When it comes to PHP frameworks, ZEND, CodeIgniter were my favourites. I used CodeIgniter for simple applications and ZEND framework was the giant who saved my good hours. But I never enjoyed PHP with frameworks. Frameworks are good for just get things done.

Once I joined with August I got chance to work with Symfony. Not just Symfony, It’s Symfony2 :)

Symfony2 is one of a kind framework, it is not a MVC framework. If you look at the Symfony2 architecture, you could find controllers, views but no models. It has been developed as a HTTP framework.

That is how the web works.

The biggest concern in Symfony2 is decoupling and from the core it has given reusable set of standalone components to use. Simply it has provided a set of tools to develop applications.

At the beginning, I was really frustrated because it seems to be complicated and over engineered. however after few days of work I started liking how it works. The most impressive part in Symfony2 is the Bundle concept. If you write a Symfony2 bundle, simply you can run it within any Symfony2project without a hassle. A bundle contains it’s own configurations, routing rules, actions, templates and business logic codes.

Dependency injection is another powerful feature which we can commonly see in JAVA developments. That means Symfony2 has adopted lots of best things from other languages without trying to reinvent the wheel.

According the the Symfony2 documentation, it contains 21 standalone libraries which we can use for day-to-day developments.

  • DependencyInjection
  • EventDispatcher
  • HttpFoundation
  • DomCrawler
  • ClassLoader
  • CssSelector
  • HttpKernel
  • BrowserKit
  • Templating
  • Translation
  • Serializer
  • Validator
  • Security
  • Routing
  • Console
  • Process
  • Config
  • Finder
  • Locale
  • Yaml
  • Form

It is not an easy task to explain the awesomeness of Symfony2 in a single blog post :) This is going to be the very first of many blog posts about Symfony2. I’ll be writing some tutorials specially for beginners who are willing to use Symfony2 in their next projects.