Open source projects have a history and a lot of reasoning behind them thatevolves. Symfony2 has been evolving for the past several years. Many newdevelopers coming to use the framework feel the steep curve of mastering theframework and overcome it at the end, but are left with many doubts when itcomes to developing their projects in the true Symfony way.
For symfony1 there was a book that covered the symfony1 practices. For Symfony2there is the Book which is the major source for documentation. The Book has alsoCookbook entries which help somewhat. However, even then not all the aspects andpractices brewed during so many years can be summarized with those resources fordevelopers with a hunger for understanding the whys and best practices thatonly real-life field experience with Symfony can provide. There are blogs aboutSymfony development also, however they are sparse and blogs even like mineassume a lot of things about the readership and are more for experimental thingsand nuances of the framework. What has been missing up to now has been a strongtechnical opinion poured in a book catering to the community and pointing in theright Symfony way.
Matthias has had more than one year understanding Symfony from the inside outand bringing responses to the whys to the surface for us. I could not trustanyone other than a Certified Symfony Developer to do this. Seldom times have Ifelt I understood Symfony so well to use it as my very own arsenal. Reading thisbook has been one of those times. Another time was when I heard Kris Wallsmithexplaining/acting out the Symfony Security Component. This book is a must foreveryone who is eager to carefully understand the framework.
Matthias will give away in this book a lot of secrets so you will find yourselfoften coming back to the book to see how a recommendation was issued, and whatto do in your case. Matthias has also spoken about things I even did not think Iwould find in this book and he did it in a straightforward manner with examplesand code.
I truly hope you enjoy this book.
Introduction
A year with Symfony. In fact, for me its been more than a year, almost 6years even. First with symfony 1 (mind the lower case and the separate 1), thenwith Symfony2. Symfony2 is what you may call a mature framework. You can dopretty advanced things with it. And when you want to do one of these veryadvanced things, you can also choose not to install the entire framework, butjust one of its components.
Starting to work with Symfony2 meant for me: learning much about programming ingeneral and applying many of the things I only knew from books to any code Iproduced from then on. Symfony2 did this for me: encourage me to do thingsright.
In the meantime, Ive written a lot about Symfony2, contributed to itsdocumentation (specifically some Cookbook articles and the documentation for theSecurity Component and the Config Component), I started my ownblog with articles about PHP ingeneral, Symfony2, its components and related frameworks and libraries likeSilex and Twig. And I became a Symfony2 Certified Developer, during the veryfirst exam session in Paris at the Symfony Live conference in 2012.
All this is now being followed by the book youre reading now, called A YearWith Symfony. It contains many of the best practices that me and my fellowdevelopers at IPPZ discovered while working on largeSymfony2 applications. It also contains some in-depth knowledge that youll needto have when youre going one step further than just writing code inside acontroller or a template.
Thank you
Before I continue, let me take a moment to thank some people who have helped mefinishing this book. First of all, Luis Cordova, who has been following my stepssince I first started to write about Symfony2 in 2011. He did a thorough reviewof the first draft. My colleagues at IPPZ have also provided me with valuablefeedback, encouraging me to make some things clearer, and some things moreinteresting: Dennis Coorn, Matthijs van Rietschoten and Maurits Henneke. Workingwith them for two years, sharing concerns about maintainability, readability,reusability, and all the other -bilities, like laughability has been great fun.Thanks also to Lambert Beekhuis, organizer of the Dutch Symfony2 Usergroupmeetups, for giving me some valuable feedback with regard to my English grammar.
Who should read this book
Ive written this book with developers in mind who have worked with PHP for sometime and with Symfony2 for a couple of weeks, maybe months. I assume you haveused the official Symfony2 documentation to get acquainted with the basics ofcreating Symfony2 applications. I expect you to know the basic structure of anapplication (the standard directory structure, the way you can create orregister a bundle), how you can create controllers and add routing configurationfor them, how to create form types, and write Twig templates.
I also assume you have worked with some kind of persistence library, be itDoctrine ORM, Doctrine MongoDB ODM, Propel, etc. However, in the book I onlyrefer to Doctrine libraries, to keep things simple. When you use some otherlibrary for persisting objects, you are likely able to find out how to applysome of the concepts described in this book to code written for anotherpersistence library.
Conventions
Since this book is explicitly about Symfony2, from now on I talk aboutSymfony which looks a bit nicer.
Everything I say about Symfony is related to version 2. Ive written and testedthe code examples in this book with Symfony 2.3. However, they may very wellalso be applicable to Symfony 2.1.* and 2.2.* and maybe even to Symfony 2.0.*.
In this book I show pieces of code from Symfony itself. To make this code fit ona page and still be readable, I have sometimes modified it.
Overview of the contents
The first part of this book is called . It will take you along from thefirst entry point of a Symfony application in the front controller to the lastbreath it takes before sending a response back to the client. At times I willshow you how you can hook into the process and modify the flow or just changethe results of the intermediate steps.
The next part is called . It contains a collection ofpatterns that are solutions to recurring problems when it comes to creating ormodifying service definitions, based on a bundles configuration. I will showyou many very practical examples that you can use to model your own bundlescontainer extension, configuration class and compiler passes.
The third part is about . I suggestvarious ways to get your controllers much cleaner, by delegating actions to formhandlers, domain managers and event listeners. We also take a look at state andhow to avoid it in the service layer of your application.