Hi there,

Welcome to my tutorial on how to build your own custom framework using PHP and Symfony components. This tutorial will be made up of a collection of posts, which aims to closely describe the process of combining different sets of components into a complete (and hopefully) easy to use framework.

Right off the bat, I would like to give you a brief history of how this tutorial came to be. You will learn what is Symfony and some reasons of why you might want to go through the pain of creating your own framework. At last, I will properly introduce my existing framework (Hero) and close up this post with an outline of the entire tutorial. Notice that by the time you hit this post, the tutorial might still be in the making, so please consider subscribing in order to get notified.

Some history…

A couple of years ago I began working on a large project, which involved creating a Point-Of-Sale system to be used by a large number of different clients in Shanghai. Different than the other projects that I usually worked on, this did not involve building the system from scratch, but rather building on top of a pre-existing and pre-historic code base. Not only was this code impossible to test, but it had also been written in such a manner that HTML, Javascript and PHP were mashed together like the boiled rice grains at the street food joint outside my office.

Eager to escape legacy code hell, I rolled up my sleeves and began the long and tedious task of refactoring the code into the MVC pattern we all know and have learned to love. I spent months chopping my way into the code and working pretty much every day of the week (sometimes until 2 AM) until finally we had something I could live with. My newly created framework was by far not perfect, but it neatly separated all the Javascript/HTML (which earlier had been echoed directly from PHP files) and isolated all the database logic into models. The controllers classes were found and called by parsing the url path.

As the code began to take shape and the system became stable, I found myself looking for ways to improve our user experience by implementing things like form validation and translation (Chinese and English). Already burnt out from the tedious work of the past months, I opted for not re-inventing the wheel this time and spent hours googling fiercely for existing PHP form validation and translation libraries. It was around this time that my google searches led me to the Symfony components page…

Of course, as a freelance PHP developer I had heard of Symfony before and had run into its Http-foundation Component when developing with Laravel. The thing is, I never really bothered to look into it and had never really directly used it in any of my previous projects. Now, looking at it more carefully, it was ridiculous how mediocre my framework actually looked. I immediately had one of those “Hallelujah” moments when the light of the All Mighty shines upon thee. Needless to say…I now had yet another framework to re-write and I wasn’t gonna get any sleep until I did it.

By the time I started replacing my previous custom framework with Symfony components there was already a lot going on. We had a fully functioning user & groups login system, well-established controllers classes and a good way of mapping URL paths to these classes. I needed to find out how to implement the routing, security, templating, etc. It took me months to fluently find my way around the inner workings of the Symfony Security Component and Routing. The experience I gained in these months is now yours to read…

What is Symfony?

If you have been coding PHP for some time, then there is a good chance that you’ve already heard of Symfony. For those of you who are new to PHP development: Symfony is a PHP web application framework and a set of reusable PHP components/libraries. First released in 2005, it aims to speed up the creation and maintenance of web applications and to replace repetitive coding tasks.

As a matter of fact, even if you have never directly worked with Symfony, you might have come across some of its components when using other popular frameworks such as Laravel or Drupal. Symfony is sponsored by SensioLabs, a French software developer and professional services provider. The first name was Sensio Framework, and all classes were prefixed with sf. Later on when it was decided to launch it as open source framework, the brainstorming resulted in the name symfony (being renamed to Symfony from version 2 and on), which still matches those theme and class name prefixes.

Why create your own framework?

At this point you might be asking yourself why you should go through the trouble of creating a framework from scratch, when you can easily start building with any of the dozens of pre-existing frameworks out there: Symfony Framework, Laravel, CodeIgniter, etc…

In my opinion, the strongest reason for creating your own framework from scratch is to learn. By creating your own framework, you will be forced to make architectural and structural decisions. The more you exercise making these decisions, the more you will become familiar with the different design patterns out there. Not to mention that if you are just starting out in PHP, this exercise will allow you to understand when to use interfaces, abstract classes, etc.

The second strongest case for building your own PHP framework would be flexibility. Now, most PHP frameworks out there are already pretty darn flexible, but as Steve Jobs once said: “The world and everything around you was created by people no smarter than you”. So if you are not happy with the way something works, then you can change it!

Let’s build it together: Introducing my Hero Framework

In a nutshell, I will be showing you how I built the custom framework, which I use on most of my current projects. It will be a very simple and high-performance framework, which borrows from Symfony  and Laravel. The foundation stone of this new custom framework is Symfony’s Http Foundation and the building blocks are Symfony’s standalone components.

No framework is finished without a security layer to log users on and off the system; as well as a routing mechanism to map URLs to controllers within the framework. The Symfony Framework has its suggested way of registering routes and I don’t doubt its great, but I prefer the Laravel way, using a separate routes.php file to register all the routes in one place. In the last chapters of this tutorial I will introduce these two very essencial topics in our custom framework.

Since this is thePHPHero.com and I am the PHP Hero, we will be naming this project: The Hero Framework. In the section below, you can see how this tutorial has been divided. Please feel free to skip parts of it if you are only interested in some very key topics.

Tutorial structure

  • Part 1: Introduction [Your are soaking in it]
  • Part 2: File structure
  • Part 3: Bootstrapping
  • Part 4: Http foundation
  • Part 5: Http kernel
  • Part 6: Routing
  • Part 7: Security

  • Snail

    Thank you so much, i was searching for an article like this but i didn’t found.
    I read Symfony but that are for advance user.
    Waiting for next article.