Symfony Components in a Legacy PHP application

A set of snippets showing you how to integrate Symfony Components (Routing, Forms, Validation, DependencyInjection, VarDumper, ..) in a Legacy PHP application

Symfony Components are a set of decoupled and reusable PHP libraries. They are becoming the standard foundation on which the best PHP applications are built. You can use any of these components in any of your applications independently from the Symfony Framework.

You can read more about these components on the Symfony website.

The purpose of this post is to roughly describe how to implement some of the Symfony Components. I've created a set of gists to get started. You should already know how Symfony Components work in the Symfony Framework. Make sure you know how to fill up a services.yml, add form validation, add translations etc. If not, you should dive into the Symfony Docs of the Symfony Book.

 

Why?

We all know the pain of legacy code structure and/or frameworks. Adding Symfony Components will add some freshness, could kickstart the development process and possibly boost your productivity. Symfony Components will future proof all your to-be-implemented features for a (hopefully) full Symfony migration in the future.

Composer packages

Bootstrap a container

An example on how you can bootstrap the Symfony Dependency Injection Components and create a ContainerBuilder. You should match this with your own legacy application.

Define all services

This services.yml defines a whole set of new services and factories. A Twig template engine, preloaded with forms extension, translation extension and routing extension. A controller resolver which reads routes from a routing.yml and looks for the correspoding controller and action. It also has the easy formbuilder with csrf protectionvalidation (annotations), and http extension. Thanks to the expression engine we can use magic like "@=service('validator').getValidator()"​.

An example routing.yml shows how you can use routing.

Example: Controller resolver

How to resolve the controller from your routing.yml by using the current Request. Remember to setup your webserver as you would for a Symfony application, more information about this can be found the Symfony website.

Example: FormBuilder

How to create a simple form, more information about the builder can be found in the Symfony docs. With the services.yml above you can also use Validation on your model with annotations, validation message translatations can be injected in the translator component. Also csrf protection on the form can be enabled, which you should! A custom form theme is loaded from "Form/form_div_layout.html.twig​".

Example: Console

A very (very) basic example on how to use console commands. You could create the Application and Commands in your services.yml and inject the container in these commands. Or you can create a custom CompilerPass that injects these Commands into you application when a service is tagged as command.

Example: VarDumper

An easy example, since the VarDumper component is autoloaded by the composer autoloader. 

Wrap up

These examples are very basic, you can do much more when combining all kinds of Components.

Feel free to leave your tips, hints and comments!