Jumping into Symfony 2

19th July 2014

The first framework I touched was Zend Framework 1 and moved onto Zend Framework 2 when the beta came out. It has served me well during numerous of projects, but I couldn't help but wonder what Symfony 2 is like as opposed to ZF2. The best way to find out is do a project with it, right? That's exactly what I did.


My brother is a professional chess player and once a year he co-organizes a chess tournament called "kroeglopers toernooi" (literally translated: pubwalkers tournament). It's a tournament which takes place in several pubs around town, during the tournament you play matches in different pubs in a team of 2 while drinking lots of beer. The problem with this was that someone had to go to all the pubs to write down the results and people had to walk back to the main pub to find out who they're up against for the next round. Along came the idea from my brother to create an app which could effectively solve this problem.


During the making of this app I came across a few nifty features which, to me, made Symfony 2 stand out from ZF2.


There are multiple ways of defining routes in your SF2 application, through YAML configuration:

# app/config/routing.yml
  path: /blog/{slug}
  defaults: { _controller: AcmeBlogBundle:Blog:show }

But you can also choose to do this using annotations:

 * @Route("/blog/{slug}", name="blog_show")
public function showAction()
    // ...

After taking a quick poll on IRC, people told me to definitely use the annotations method, it took some getting used to but it's a breath of fresh air coming from ZF2's config hell.

Controllers as Services

Since I got introduced to the concept of Inversion of Control when starting with ZF2, I've been reading up on it and found that things like the Service Locator pattern are often seen as an anti-pattern (rightfully so). Since I strive to write code that is clear and explicit, I chose to stay away from Symfony's Service Container as much as possible.

Symfony's concept Controllers as Services is the perfect way to write controllers that have explicit dependencies and also gives you a headstart on creating framework agnostic controllers.

Turning controllers into services allows you to configure which dependencies should be passed onto the constructor when being instantiated and you can drop the Base Controller.

Form Collection

Form Collections make it possible to create a form which contains a collection of forms, this way you can easily create a form which allows you to create a round with multiple games and where each game has two teams with each two players. I wish I could say it was a breeze to create this scenario however, it took a lot of trial and error to get to where I wanted to be. The biggest advantage is that Symfony offers you a non-hacky way to achieve this, without having to use third-party bundles.


The first few things I do with Zend Framework 2, like installing Doctrine and Twig, is already done for me in Symfony 2. That alone gives it a step ahead of ZF2 from my point of view.

I'll definitely be picking Symfony 2 more when I get the chance, it's a great framework and has proven to be very flexible. I feel like Symfony 2 (or its components) is adopted way more than any other framework, gaining knowledge of Symfony 2 make a lot of other frameworks and platforms easier to start with. Take Bolt CMS, OroCRM and Silex for example; they're all based on Symfony 2 (components).

I'll be posting more in-depth articles on my new experiences with Symfony 2.

comments powered by Disqus