A Look at PHP 5.3 Frameworks – Symfony2

Posted on March 14, 2011

Ever since PHP5.3 was released we have seen several new frameworks pop up in the PHP community. In this series of articles we will look at and evaluate some of the next generation PHP frameworks. Today we look at Symfony2.

Overview

Symfony2 is a full stack web enterprise framework created by well known PHP guru Fabien Potencier. The framework itself is divided up into several different packages (Bundles) which are tied together with a Kernel object. Being a full stack framework Symfony2 offers almost everything you will need out of the box:

  • Model, View, Controller separation
  • Database management with Doctrine2
  • Templating with either straight PHP templates or Twig
  • Testing with a layer built upon PHPUnit
  • Emails with SwiftMailer
  • Custom form, validation, auth, event and caching systems.

One thing that distinguishes Symfony2 from other frameworks is that it relies heavily on Dependency Injection and as a result alot of configuration needs to take place. The standard of the Symfony2 is top notch, it is clearly set out, commented and tested. The full stack nature of Symfony2 is also a big strength. If you were to start a high upkeep project, you could be sure that in 5 years all components of the stack (forms, validation, templating, testing, database) would still be maintained. An overview of the architecture of a Symfony2 application can be found here.

PHP, XML or Yaml?

One of Symfony2′s unique features is that it allows you configure the majority of your application in either PHP, XML or Yaml. When Symfony2 is running in production mode, all XML or Yaml files are compiled and cached as native PHP files, so there is no performance advantage gained by using straight PHP. Frameworks such as Zend Framework only allow application settings to be defined in XML, Yaml or .ini format, Symfony2 goes a step further and allows you to specify functionality such as validations and url routes in these formats. For example, the following code snippets show how to validate the presence of the $name attribute in the Author object below.

// Sensio/HelloBundle/Author.php
class Author
{
    private $name;
}

PHP

// Sensio/HelloBundle/Author.php
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Components\Validator\Constraints\NotBlank;

class Author
{
    private $name;

    public static function loadValidatorMetadata(ClassMetadata $metadata)
    {
        $metadata->addPropertyConstraint('name', new NotBlank());
    }
}

Yaml

# Sensio/HelloBundle/Resources/config/validation.yml
Sensio\HelloBundle\Author:
    properties:
        name:
            - NotBlank: ~

XML

<!-- Sensio/HelloBundle/Resources/config/validation.xml -->
<class name="SensioHelloBundleAuthor">
    <property name="name">
        <constraint name="NotBlank" />
    </property>
</class>

source

Bundles

Bundles are packaged classes and methods which provide functionality for your application. Symfony2 ships with bundles that are required for the framework to run. These are things like DoctrineBundle (database), TwigBundle (templating), SwiftmailerBundle (emails) and ZendBundle (Zend Framework integration). Code inside bundles must follow the defined structure and naming guidelines, specified here so that Symfony2 can automatically know where to find controllers, tests and translations. Bundles are what other frameworks would call plugins, modules or extensions. You can download Bundles developed by the community that provide features like user registration, Facebook integration and blog functionality from symfony2bundles.org.

Testing

As you could expect with any enterprise framework, Symfony2 comes with complete testing functionality. Symfony2 provides a layer on top of PHPUnit for you to define your tests. You can write standard unit tests (as you would with PHPUnit) as well as ‘functional tests’. Functional tests are essentially automated front end testing, Symfony emulates a browser request to a specific URL of your application and you define what output you are expecting to see. This could be a specific HTTP response code or some HTML on the page.

HTTP Caching

Every Symfony2 controller returns an instance of the Response object. The Response object has several methods available that allows the developer to set specific HTTP headers which are then sent to the browser. This allows the developer to have complete control over every header that is being sent back to the browser. The slides below show how powerful this feature can be.

Community

Although Symfony2 is still in pre-beta, the majority of functionality is already in the framework. All official documentation and guides can be found on the Symfony2 website Symfony-Reloaded.org. If your a twitter user than I recommend following @fabpot (creator, developer of Symfony2), @jwage (developer Symfony2, Doctrine) and @symfony (aggregated Symfony news). An RFC was recently posted on the phpBB4 wiki suggesting that phpBB4 be built using Symfony2. If it gets accepted then we could expect the thousands of mod developers for phpBB2 & 3 would learn Symfony2 while developing mods for phpBB4.

Conclusion

I suspect developers with previous experience in Symfony1, Java and .NET would find Symfony2 perfect and won’t hesitate to use it. Those who use PHP frameworks such as Code Igniter, Kohana, CakePHP and Yii will find Symfony2 very different. The strict naming convention, long namespaces and dependency injection may put them off. If phpBB4 does decide to use Symfony2 then other open source PHP projects may follow and Symfony2 will no doubt become the most popular PHP framework. Regardless of developer preferences or immediate success, Symfony2 is an amazing piece of code and I highly suggest you try it out.

Links

Leave a Reply

You must be logged in to post a comment.