Overview of the Symfony PHP framework

I have always developed my PHP sites with just PHP and a collection of classes I put together myself to make database interaction and other tasks easier. This has always seemed to me to be the simplest way to do things. But web frameworks are becoming increasingly popular and one of the latest projects I'm working on is being developed with the Symfony PHP framework.

Being new to the world of frameworks, I have found there is a steep learning curve for Symfony. Especially at the beginning, I felt as though way too much complexity was being thrown into the mix. It's been a couple of weeks since I first took a look at Symfony and things are seeming much more sensible now. Still, I find the claims by Symfony's creators that it is simple and fun to use to be somewhat cringe-worthy!

Symfony can be obtained for free from the project's website I tried both the Ubuntu-specific install and the Pear install and they both worked without a hitch.

Symfony comes with a command line tool that is used to create and maintain projects and applications. In Symfony-speak, an application can consist of one area or section of a website, such as a frontend or a backend. Once the most common commands of the tool are learned, it becomes a useful way to ensure that your project is properly structured and set up.

Like many web technologies nowadays, Symfony uses the MVC pattern to split an application into three different layers: model, view and controller. The way this is done in Symfony means there are lots of files scattered all over the place, which at first is overwhelming, but becomes intuitive when you learn where to look for template files, action classes and model classes.

The model is where the data is accessed and modified and where the business logic resides. Object-relational mapping is done through the use of the Propel library, and database abstraction is achieved through the use of the Creole library. The business logic can be added by the programmer to the default logic for dealing with database objects created by Propel.

The view uses templates written in HTML and standard PHP, though an alternate syntax is recommended for the PHP fragments to make them easier to understand for designers. Several more advanced view techniques are used to split elements of a page in clever ways that enable caching, reuse and grouping of sections of pages together. These have names such as partials, slots, components, component slots, and so on. The template created for a page is "decorated" with a global layout before being shown to the user.

There is a single front controller created by the Symfony CLI tool, which is not modified. Instead, the view layer is tied to the model layer via what are called actions. Actions are placed in a PHP class where they are automatically called upon in the right context. Actions belong to modules and a module will typically have one or more actions, but usually less than 10 or so.

A lot of the features in Symfony are configurable. The configuration language of choice is YAML, and you will see many files ending in .yml in Symfony's config directories. YAML uses indentation rules for its structure and can accommodate nested arrays as well as simple variable settings. An example of Symfony feature which uses configuration is the routing system, which links the external URLs of an application to the internal module/actions they are matched to.

Templates can rely on the use of helpers, which are functions that make generating commonly used HTML easier. An example helper is the link_to function, which generates an HTML anchor tag. You can also create your own helpers for use within templates.

Symfony provides different environments in which your application can run. For example, there is the development environment which includes a debugging bar across the top of all pages on the site, and the production environment which is the one meant to be accessed from the internet. You can alter the configuration of the environments such that they use different databases (amongst other things) which keeps testing data nicely separated from live data.

Symfony uses a routing system to map URLs to the modules & actions that will be executed. Configuration here is also done in YAML and is a nice alternative to using the Apache .htaccess files (which Symfony itself makes use of to rewrite page requests to its front controller).

You can extend Symfony's out-of-the-box functionality with plugins, which you can write yourself or obtain from the project website and elsewhere. Implementing a feature as a plugin makes it reusable across different projects, including others' projects if you decide to release it to the public.

The Symfony CLI tool can be used to generate the scaffolding of pages and modules for you. For example, it can be used to generate a CRUD for an articles section, allowing you to create, read, update and delete articles. Also, it can generate a backend administration section too. You can then modify the generated code to tailor it to your own requirements. The generation is cleverly based on the database & model layer classes.

There are plenty of online Symfony tutorials to get you started, some of which are included on the Symfony website, alongside screencasts of tasks such as implementing Ajax and creating an administration section for your site. In addition to this, a book, The Definitive Guide to Symfony, is available online for free.

So, in conclusion, I have much left to learn about Symfony but so far I am enjoying using it to get things done quickly and in a highly organized and standardized way, such that other Symfony programmers should be able to take over the reigns of my Symfony projects with ease.