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.
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.
- 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