Preface - Extending Symfony2 Web Application Framework (2014)

Extending Symfony2 Web Application Framework (2014)


The first stable version of Symfony2 was released more than two years back. Coming from all the experience acquired from Symfony1, the promise was to remove all the magic and provide a solid and modular basis to build web applications. The trade-off's inconvenience was justified in order for developers to regain full control and knowledge of the working of their application. To achieve this, it was decided that everything would be a bundle. The core framework itself is just a collection of bundles, which is everything you need to get started.

This great architecture being at the heart of Symfony2 and the promise of greater modularity and control over the whole framework enables any developer to create their own extensions. It is easy to implement these extensions; everything is prepared so that these extensions can be shared and their configuration can be disclosed for other developers to use them.

From the basics of creating a simple service to a custom authentication, this book will guide you through everything you need to create amazing bundles for Symfony2 and share them with the community.

What this book covers

Chapter 1, Services and Listeners, talks about the services and listeners that are the basis of nearly all extension techniques used in Symfony. This covers the fundamentals that will be reused throughout the book.

Chapter 2, Commands and Templates, helps you make your templates smarter and augment them with your own tailored functions and filters. This chapter helps you wrap common actions in commands so that you can perform them easily and reliably.

Chapter 3, Forms, helps you create your own form types and widgets and use them inside of dynamic forms that change based on the user information or even their own input.

Chapter 4, Security, discusses how to write custom authentication methods, use voters to restrict access, and add additional security layers to Symfony2.

Chapter 5, Doctrine, describes how to make your database fit your data and not the opposite. This chapter also describes how to write custom database types and extend Doctrine to easily share common domain logic between models.

Chapter 6, Sharing Your Extensions, helps you to create a great extension that others could benefit from. It contains everything you need to know about publishing a self-contained reusable bundle.

What you need for this book

You will need a working Symfony environment behind a web server (Apache, Nginx, and so on) and a relational database server such as MySQL or PostgreSQL. Some examples are based on using MongoDB but can be applied to other databases as well.

The book makes use of features available in Symfony 2.3 and higher. The examples might have to be adapted a bit if you are using an older version.

Some code also makes use of features available only in PHP 5.4 or higher, so they will need to be adapted to work with older versions of PHP.

Who this book is for

This book is for you if you fulfill the following conditions:

· You are already using Symfony2 and PHP

· You want to understand more about how it works under the hood

· You need to replicate some of the Symfony2 core features but ones that are tailored to your specific needs

· Your controllers and models are growing out of control

· You need a better way to structure and organize your application logic and code

This book is not for you if you are just getting started with Symfony2. It will confuse you more than it will help you. Keep it on your night stand for a while and come back to it later.


In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text are shown as follows: "The php app/console container:debug <service_name> command will provide information about a specific service."

A block of code is set as follows:

use Geocoder\HttpAdapter\CurlHttpAdapter;

use Geocoder\Geocoder;

use Geocoder\Provider\FreeGeoIpProvider;

public function indexAction()


New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "If you have enabled colored output in your console, the line saying Success! should appear in green."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.