Compatible code: starting with symfony2

Warning: This blogpost has been posted over two years ago. That is a long time in development-world! The story here may not be relevant, complete or secure. Code might not be complete or obsoleted, and even my current vision might have (completely) changed on the subject. So please do read further, but use it with caution.
Posted on 01 Dec 2011
Tagged with:

Because learning new stuff is just one of those things I need to do on regular basis, I’ve decided to dive into another framework than the ones I’m used to. Having dealt with mostly Zend Framework 1 on a daily basis, and CodeIgniter which is the one I deal with a lot inside the Joind.In project I’d like to contribute to, I’ve decided to give another framework a chance. A framework that is on the shortlist for a long time now: Symfony 2. The tl;dr: winning!

First of all: this is not a comparison of frameworks. That might be tempting to do but we’re talking about comparing apples and oranges here. If we need to do a comparison we need at least ZF2 against Symfony2 or ZF1 against Symfony1. Since I don’t have enough experience (yet) with ZF2 and not enough experience with SF1 (and probably never will), I cannot realistically make a decent comparison. I think there are enough (biased) blog-posts about that subject already and if not they will be available as soon as ZF2 hits its first official release. This blog-post is about the journey I’m currently making on discovering a framework which pretty much mimics the way I would setup a framework.

Code that suits you, and code that will not.

Maybe it sounds strange to some but I find that some code I see is written in a more sensible way for me to comprehend than others. It doesn’t mean that the other code is wrong, it’s just that it isn’t my  cup-of-tea. It doesn’t make sense the way things are written even though it’s a correct solution to the problem. See it as being attracted to certain kind of women (or men): you can be attracted to redheads, but less to blondes (really? :P), or the other way around. It’s your own preference.  Last year I needed to make a quick tool that had to communicate with some code inside our ZF1 project, so I decided that we could use Zend_Tool for the job. Probably most would agree, but it turns out that after a few hours of work I still was at lost with Zend_Tool.  I agree 100% with everybody here: I don’t know enough about Zend_Tool so I can’t blame Zend_Tool for my failure. I don’t.

But a friend of mine who is a big supporter of Symfony told me to give Symfony1 a shot. Off course I was skeptical since I knew less about symfony1 than I did on Zend_Tool. That, plus the fact we needed to autoload, bootstrap and get two frameworks up and running simultaneously. What could possibly go wrong! Conclusion: I’ve got my tool up and running about a 45 minutes later…

What did happen?
I downloaded symfony1, did the tutorial on getting it installed, found a blog-post about symfony tasks (the zend_tool counterpart) and started to work on it. Somehow, Symfony did things almost exactly on how I would do things myself. I needed to do some work on copying skeleton-directories and replacing variables, and it turns out it was pretty much how I would do it so it made sense to me. Same goes for creating my tasks and some other stuff I needed to do. I didn’t had to look everything up because the code I needed was there were I would though it would be (not always, but most of the time). It made me develop my Symfony Task as fast as somebody who got loads of experience with it…  it was amazing! Plus, thumbs up for both frameworks to be able to bootstrap Zend framework in order to use their database connectivity inside Symfony. It all worked!

In the end I was really excited by how Symfony was written, but alas, time did not permit me for diving into the framework itself. But the few times I took a peek later on, I saw things that made me go: “you know, this all makes sense!”. I really hope such a thing has happened to you before. Not actually struggling with code, but 3rd party tools helping you pretty much as if you had written the code yourself.

So, symfony2 is the same deal, right?

So with good hopes I’ve downloaded the latest symfony2 release, looked at the installation and start running. The first thing that I noticed is that symfony2 is just there for you. It comes with a setup/configuration tool running as soon as you connect after you unpacked the source, it will guide you through different stages and when there are errors, it will tell you what’s wrong and not just a plain: error.

Now does this mean that symfony2 is for dummies who can’t think for themselves? I believe the opposite is actually true. The developer-mode (app_dev.php) has a toolbar at the bottom of the screen with all information about the current request: which controller, HTTP status codes, status/name of who is logged in, timing and database query-count etc. More information is available by clicking on the details. This bar only saves so much time when writing code. Of course, everybody (no exceptions) should have at least a debugger present and should be able to set breakpoints instead of just var_dump()’ing or die()’ing with by hacking those statements into the code, but having this info directly on your screen saves so much time in the end.

I started with symfony2 by just following the blog-tutorial. I noticed most of it was pretty obvious (not everything though, but it made sense quickly enough), and a few hours later I could write my own models, controllers, doing authentication on routes etc. I find it that symfony2 is just as symfony1: I can relate to the code and it makes sense. But not everything though, lot of things still are strange and I need more time to figure out how they work. The DI-approach looks great and I find it’s a lot easier to move around the core in order to understand how things work. It means I understand it faster, and I will be able to use it more quicker.

Can’t wait to do use symfony2 in something else than just hobby-projects (which probably won’t be far off, more on that later)..


Symfony2 is great. I’m sorry for all other frameworks out there, but at this moment symfony2 is lonely at the top of my list. I really hope ZendFramework2 will be just as awesome and according to Matthew, Rob Allen and others I’ve spoken about the framework it probably will be. I think it will be nice to have more frameworks around that actually helps you develop faster from day 1, which in the end is what frameworks are all about. Symfony2 does that for me, but there are many others, and everybody has their preference, which goes to show that choice is a good thing.