PHP Framework Comparison: Symfony
Thursday, July 10th, 2008This post is the second in a series. The first post focussed on the strengths and weaknesses of the Zend Framework. This post about another leading PHP framework, Symfony.
Note: This post refers to Symfony 1.0. Version 1.1 has recently been released, but I have not had time to examine it in detail, though I imagine that most of my comments here apply equally to the new version.
Superficially, Symfony reminds me of Django. This is a Good Thing in my book. Symfony is a framework that is clearly designed to get applications up and running quickly, with a good ORM system and the facility to generate basic code automatically. The contrast with ZF is quite clear: Symfony is a tool that makes building web applications quick and easy as a top priority.
Features
Symfony provides all of the things that one would expect from a modern web framework, and a few more besides. The MVC components are all there – model development is supported by an ORM system that uses the Propel library, although there’s nothing to stop you using another library, such as Doctrine, instead (in fact, this is quite commonplace, and there is a well-supported plugin for Symfony that does just that). The controllers (’actions’ in Symfony parlance) are supported via a base class that provides useful abstractions over the HTTP request/response process. Views are rendered automatically for successful actions, with ‘partial’ views allowing sub-elements of a view to be templated, and ‘components’ fill a gap between heavyweight action-view combinations to provide ‘widgets’ which can be reused in different views throughout your app. Various helper functions save time and code by generating common HTML elements for you.
A lot of time spent on small Symfony projects will be spent editing configuration files as much as writing code. Some people might not like this, but I found it to be inrcredibly useful. Form validation, for example, can be set up using configuration files (although you can extend the basic set of validation options using custom classes). More importantly, your data model is defined using a configuration file which specifies your tables, their contents, and the relations between them. Once you have created your schema file, you can run command-line scripts to auto-generate the PHP class files that form the basis of your model. For example, for a table called ‘Posts’, Symfony (or, more accurately, the Propel library) will generate a PHP class called ‘Post’ which you can edit yourself, but this class inherits from the class ‘BasePost’ which provides getter/setter functions for the fields, as well as functions to retrieve related objects. Also created is the PostPeer class which provides (mostly static) functions to retrieve Post objects from the database. From here, you can run further commands which will generate basic CRUD modules – you can actually have a functional data-entry app without writing a single line of code. It will rarely be useful for production purposes out-of-the-box, but it provides a good foundation to build from.
There are plenty more configuration files, controlling database connections, caching, routing (URL paths), logging, security (access control) and default view settings, amongst other things. This approach won’t be to everyone’s tastes, but I have to admit that I liked it. One wrinkle is that many of these configuration files are in YAML format – a kind of strange hybrid of INI files, XML and Python’s whitespace handling that has been used in the Ruby on Rails. Again, it’s a matter of personal taste, but I found these files to be more readable than XML and certainly a lot less typing is involved in editing them.
Documentation
The documentation for Symfony is awesome. It is, quite simply, the best set of free documentation I have ever read for any software library, ever. The documentation on symfony-project.org consists largely of three main parts: The Book, the API reference, and Askeet. The Book, as the name suggests, consists largely of the content which makes up The Definitive Guide to Symfony, by Fabien Potençier (CEO and lead developer at Sensio Labs, where Symfony was created) and Francois Zaninotto, until recently the head of the Symfony documentation team. The Book is an invaluable reference to the core features of Symfony, and provides all of the introduction that anyone could need. The API reference provides a comprehensive backup for when you need the precise details, although it could be slightly easier to navigate. And, as if all of that were not enough, there’s ‘Askeet’, a 24-part mega-tutorial, in which Symfony is used to create a whole, functioning web 2.0 app, in easy-to-follow stages. Each stage represents a new achievement, with an emphasis on getting a new working piece of functionality in place. Often, code from earlier stages is substantially refactored during later ones, providing a good example of how to go about transforming a prototype into a final application. The bottom line: Symfony’s documentation rocks.
Community
Symfony’s community is not huge, but does seem to be fairly active. Symfony seems to follow the ‘benevolent dictatorship’ model of open source development, as exemplified by Linus Torvalds‘ leadership of Linux or, in the PHP realm, Dries Buytaert’s leadership of the Drupal project. Fabien Potençier appears to be the overall head of the Symfony project, although there are other major contributors. Symfony seems to have a particularly strong set of plugins – code modules which extend or enhance the framework, or provide substantial units of functionality which can be plugged in to Symfony apps. There certainly aren’t as many Symfony plugins as there are, say, Drupal modules, but this is a strength that does not seem to be present in many other PHP frameworks. The community is also active in contributing documentation, with Andréia Bohner’s Symfony cheat sheets being a shining example.
Conclusion
I have to admit, I’m now something of a fan of Symfony. There is a streak of common sense running through the way it works which makes it very fun to work with. The documentation is fantastic, and the community looks strong enough to support development for the long term. The only dark cloud on the horizon is the fact that questions have been raised by some long-standing community members about the direction that the newly-released version 1.1 is going in, suggesting that it may be sacrificing some simplicity and ease of development for a slightly more ‘purist’ approach to code structure. Time will tell whether or not these fears are well-founded but, for now, I think that Symfony is worthy of recommendation.

