Symfony 2 Walkthrough
Sensio unveiled the first preview releases of the Symfony 2 framework a while ago (note the capital ‘S’). I couldn’t help but think, great, and just after I was getting the hang of symfony 1.4. At the time I was converting from CakePHP to symfony in search of more flexible routing, but even now I appreciate Symfony 2 even more than the symfony 1.4 legacy version.
In general, Symfony 2 is much lighter and faster than all of the other PHP frameworks I’ve used (including CakePHP, Zend, and CodeIgnitor). At the time of writing, the current version is 2.0.0 Beta 3, and as I have learned more about this revolutionary redesign, I have come to understand more about the inner workings of the Internet rather than relying on “automagic” components. Symfony 2 offers a way of dealing with HTTP requests and responses in a familiar, object-oriented, MVC fashion. I won’t bother to explain the exact details, but I will summarize:
An HTTP Request begins its journey when the user performs an action on a web page. For simplicity, suppose the user clicks a link to an “About” page and makes a simple GET request. In a Symfony 2 application, the request is caught by the front controller, a single file that handles all requests and passed to a Kernel for processing.
The Kernel analyzes the request and matches it to a route defined in a cached configuration file. If the route matches a firewall pattern protecting a certain resource, then the user may be asked to provide authentication credentials (i.e. redirected to a login page). The route redirects the response and its parameters to a controller action that resides in a “Bundle”
The Controller handles the request and performs any logic to gather information from a database (if necessary). At this point, the “Model” paradigm has been replaced by Entities—plain-old standalone PHP objects that can be saved (or persisted) to a database by an external entity management service (in Symfony’s case, Doctrine). Symfony 2 also provides support for the Document storage design popularized by the MongoDB database engine.
After performing all necessary logic and processing, the action returns a response object, which could be anything from a redirection to a JSON file to an HTML page. This response is handed back to the client’s web browser.
Traditionally, HTML responses are rendered in templates. Symfony 2 uses a new templating engine called Twig, which uses syntax highly similar to the Python Django framework. The Twig is rendered as PHP and then cached for easy reuse.
Without any additional logic, a simple GET request in Symfony 2 takes less than 50 milliseconds to process, render, and return a response. A complex POST request that persists information to a database and fetches more information takes slightly longer: usually less than 100 milliseconds.
The Symfony 2 documentation offers more information, and I highly recommend reading the first couple parts it even if you aren’t a web developer. If, on the other hand, you are a web developer, Symfony is insanely fast, easy to learn, and powerful/flexible enough to customize your own core components.