Skip to content

Symfony2 – Creating Your Own App

So you want to create a new application in Symfony2? Great! Now lets do it.

Make sure you have Git installed and working. If you don’t know then check Git Installation

Once you have Git installed we can proceed. I expect you are using a Unix based system. If you are using Windows, some of the steps might not work.

Step 1

First we need to get the Symfony2 Bootstrapper.

git clone git://github.com/symfony/symfony-bootstrapper.git

Step 2

Now create a directory in your webroot for the Symfony2 application. Once done use the Phar file in the symfony2-bootstrapper to create the new Symfony2 application.

mkdir myFirstSymfonyApp
cd myFirstSymfonyApp
php path/to/symfony2-bootstrapper/symfony.phar init --name="App" --format="php"

As you can see I am using PHP for the config file format. You can choose YML or XML based on your preference. Also note that the name can be changed, but “App” seems to be a suitable one.

Step 3

We don’t have Symfony2 core libraries yet. We will get them from GitHub.

git clone git://github.com/fabpot/symfony.git src/vendor/symfony

Step 4

We require a few more libraries to actually start using Symfony2. Symfony2 comes with a script to do this.

cd src
vendor/symfony/install_vendors.sh

Note that you can control the libraries by commenting the ones you don’t require in, install_vendors.sh. Wait until all the required bundles are downloaded.

Step 5

As we all know Symfony2 is still a work in progress, we need to do some modification to make this work.

First in your web folder if you have index.php, rename it to app.php. This is the one followed in the Symfony2 sandbox. (Well this is just a convention.)

Then add a .htaccess if it’s not there. If you already have .htaccess make sure it’s same as below.

  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ app.php [QSA,L]

Now we need to enable Twig. Add the following to config.php. Remember this is required for the Symfony Profiler toolbar.

// Twig Configuration
$container->loadFromExtension('twig', 'config', array(
'debug'            => '%kernel.debug%',
'strict_variables' => '%kernel.debug%',
));

A few final steps to make things look good. This will add the required assets to make the Symfony Profiler and Exceptions look good.

php app/console assets:install web

or if you wish to use only symlinks.

mkdir web/bundles
php app/console assets:install --symlink web

As a final note if your app/config/routing.php file has

$collection->addRoute('homepage', new Route('/', array(
    '_controller' => 'FrameworkBundle:Default:index',
)));

Change it to

$collection->add('homepage', new Route('/', array(
    '_controller' => 'FrameworkBundle:Default:index',
)));

Probably you should be able to start using Symfony2 now. If you face any problem feel free to comment. Happy hacking.

Categories: PHP, Symfony2.

Tags: , ,

Comment Feed

9 Responses

  1. You can publish assets using the Symfony2 console, like “php App/console assets:install” in ur case

  2. Use php app/console assets:install –symlink instead of moving the public resources.



Some HTML is OK

or, reply to this post via trackback.

Continuing the Discussion

  1. [...] This post was mentioned on Twitter by metagoto, OpenThink Labs and Wildan Maulana, Amal Raghav. Amal Raghav said: #Symfony2 – Creating Your Own App http://bit.ly/hGyKct [...]

  2. [...] http://amalraghav.com/symfony2-creating-your-own-app/ If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader. [...]