symony2 project from scratch

Posted: January 18, 2011 9:44 PM Updated: February 16, 2011 8:09 AM

In my opinion, the best way to learn something is to do it from scratch. So I always had a problems when math teacher started to show-short cuts calculations, but were actually good at them after I learned what the calculation short-cuts were replacing. So... after seeing the symfony2 sandbox application, I decided to create symfony2 application from scratch.

First, I looked at the possibility to generate application skeleton in similar way we can generate for symfony 1.4. It took me a while, before I found this neat article "symfony2 project initialization" by Henrik Bjørnskov. Fortunately, the problem Henrik was writing about when he  was writing his article, has been resolved. So, the instructions are actually quite simple, but let me repeat them.

So, before you start, you have to get (preferably recent) version of symfony-bootstrapper project. You can download it from it's github pages here: https://github.com/symfony/symfony-bootstrapper, or clone it:

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

The second is actually better choice, since you can update your bootstrapper with one command later:

Bash code
cd symfony-bootstrapper
git pull

So... we downloaed bootstrapper, let us create project. First, let's create folder for our project:

Bash code
mkdir /path/to/symfony2/project

or

Bash code
mkdir /path/to/symfony2/project -p

Where project is the folder we're creating, then, go to that folder:

Bash code
cd /path/to/symfony/project

From there run:

Bash code
php path/to/bootstrapper/symfony.phar init --name="Symfony2Project" --format="xml"

Now note, there is/was a bug in config.xml, so if you choose your config to be stored in xml, make sure, that you reorder your section to look like this:
 

XML code
  1. <app:config charset="UTF-8" csrf-secret="xxxxxxxxxx" error-handler="null">
  2.  <app:router resource="%kernel.root_dir%/config/routing.xml">
  3.  <app:validation annotations="true" enabled="true">
  4.  <app:session auto-start="true" default-locale="en" lifetime="3600">
  5.  <app:templating assets-version="SomeVersionScheme">
  6. </app:templating></app:session></app:validation></app:router></app:config>

Mind the order of app:session and app:templating. It won't work the other way. I've submitted reordered config.xml file, and this change was pulled, but that's in skeleton folder, and it isn't actually in the phar archive (I have no idea, how to create/update it... yet) so probably you'll need to introduce that change as well.

What now? You'll need to download vendor libraries. So, let us download Symfony first, you can just clone it, like Henrik wrote in his post, but since I place all my projects in git repo, it made more sense to use sumbodules (and learn how to work with it):

Bash code
git submodule add git://github.com/symfony/symfony.git src/vendor/symfony/
git submodule init
git submodule update

Symfony uses Zend_Logger, so you'll need at least that as well. It isn't shipped with symfony2, but it's enough to copy it from sandbox package. it has exactly what we need, nothing more. But if you'd like to use more Zend Framework libraries, you can clone it from github as well, just like Henrik in his post wrote.

Another library which is rather must, is Twig, you can download it from www.twig-project.org, but even if there's RC version available, it is version downloaded from Twig's master branch on github repo which works, not RC1. And again, you can clone it:

Bash code
git clone git://github.com/fabpot/Twig.git src/vendor/twig

or submodule it:

Bash code
git submodule add git://github.com/fabpot/Twig.git src/vendor/twig
git submodule init
git submodule update

I tried to comment it out, but got error, that there's anything that can use twig configuration options.

At this point your app will yell for you at the lack of Doctrine, but you can just comment it out in yours app Kernel:

PHP code
  1. class Symfony2ProjectKernel extends Kernel
  2. {
  3.  
  4.  public function registerBundles()
  5.  {
  6.  $bundles = array(
  7.  new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
  8.  new Symfony\Bundle\TwigBundle\TwigBundle(),
  9.  
  10.  // enable third-party bundles
  11.  new Symfony\Bundle\ZendBundle\ZendBundle(),
  12.  new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
  13.  //new Symfony\Bundle\DoctrineBundle\DoctrineBundle(),
  14.  //new Symfony\Bundle\DoctrineMigrationsBundle\DoctrineMigrationsBundle(),
  15.  //new Symfony\Bundle\DoctrineMongoDBBundle\DoctrineMongoDBBundle(),
  16.  //new Symfony\Bundle\PropelBundle\PropelBundle(),
  17.  
  18.  // register your bundles
  19.  );
  20.  
  21.  if ($this->isDebug()) {
  22.  $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
  23.  }
  24.  
  25.  return $bundles;
  26.  }
  27.  
  28.  //...........
  29.  
  30. }
  31.  

I know, there's swiftmailer bundle, but well... Doesn't use anything third party at the moment.

There's one last thing to do, before we can look at our fresh, clean symfony2 project:

Bash code
./app/console assets:install web/

It copies bundles assets into web/ folder just like command below did in symfony 1.4:

Bash code
./symfony plugin:publish-assets

You can of course create symbolic links:

Bash code
./app/console assets:install web/ --symbolic

But if you install bundle's assets like that, you might have problems with loading your project through some ftp clients.

Now, you have you shiny symfony2 project up and running. Now you can start to create your own controllers and build some nice looking layout.

Note:
You should install php5-sqlite package from you repo (on linux), to use Symfony's WebProfiler!

 

Grzegorz Śliwiński

Comments list (5)

Ddoftapaps July 23, 2011 9:23 AM #1
Comment deleted by moderator
back link service December 29, 2011 7:16 PM #2
Comment deleted by moderator
Soogs January 9, 2012 4:22 PM #3
Comment deleted by moderator
backlink service January 31, 2012 5:18 PM #4
Comment deleted by moderator
Defectock February 1, 2012 11:35 AM #5
Comment deleted by moderator
Add new comment

Your email will never be published

Must start with http:// or https://
Delete the reply