Table of contents
Symfony2 is a collection of autonomous, reusable libraries that are not tied to each other, which solve common problems in web development.
Instead of using low-level components, you can use a full-scale framework based on these components — Symfony2 ... Or, you can write your own. This series of articles on the latter.
Why do you need your own framework? (Why would you like to create your own framework?)
')
First, why do you want to create your own framework? After all, if you look, everyone will tell you that you should not reinvent the wheel, and use a ready-made solution. And in most cases they will be right, but there are several reasons to start developing your own framework:
- To study the low-level architecture of modern web-based frameworks, in particular Symfony2 and its components.
- To create a turnkey framework for your specific needs. (Just make sure your needs are really specific.)
- For the experiment for fun (learned and scored)
- To refactor existing applications that need more modern and well-established practical solutions.
- To prove that you can (... albeit with a little help)
I will briefly, step by step, guide you through the topic of creating your own framework. At each step, you will have a full-featured framework that you can use as a ready-made solution or as a basis for your further development. We will start with a simple and with time your framework will acquire functionality.
Later you will have a fully functional framework.
And of course, at every step you will learn more about Symfony2 and its components.
- If you do not have time to read the full cycle of articles, or you want to start developing now, pay attention to Silex , this is a micro-framework based on Symfony2 components.
Many modern frameworks base themselves as MVC frameworks. In this series of articles, we will not discuss the MVC model, since based on the Symfony2 components you can create a framework that is not based on this model. In any case, if you look at building MVC, this part will be about how to create a Contoller part in this architecture.
For Model and View it all depends on your preferences and I give you the opportunity to use any libraries (for example: Doctrine, Propel or good old PDO for Model and / or PHP, Twig for View).
When creating a framework, the main goal should be - separation into components, rather than blindly following the MVC pattern. The fundamentals of the Symfony2 components are based on the HTTP protocol specification. So the framework we create is more precisely called the HTTP framework or the Request / Response framework.
Before we start
It is not enough just to read about creating a framework. You really need to write code for the examples we are considering. To do this, you need PHP (version 5.3.8 or newer), a web server (Apache or Nginx), good knowledge of PHP and OOP.
Are you ready? Then let's get started.
Bootstrapping
Before we start thinking about our framework, we need to negotiate several agreements: where we will store the code, how we will call our classes, how we will connect / process external dependencies.
To host the framework, create a directory somewhere on your server:
mkdir framework cd framework
Coding Standards
Before someone starts a holivor about this, let's recognize that for example, it is absolutely not fundamentally what standards to use. Therefore, in a series of these articles, we will use
"Symfony2 Coding Standards" .
Components Installation
To install the necessary components of our framework, we will use
Composer , the project dependency manager for PHP. The first option, which we describe in composer.json, will look like this:
{ "require": { "symfony/class-loader": "2.1.*" } }
Here we announce that our project uses the Symfony2 ClassLoader component version 2.1.0 or higher. To download and install project dependencies, do the following:
$ wget http://getcomposer.org/composer.phar $
After running the install command, you will see the vendor directory which contains the code for the Symfony2 ClassLoader component.
- I recommend using Composer for you, although you can download an archived component or use Git submodules. You decide.
Naming Conventions and Autoloading (Naming Conventions and Autoloading)
We will use
autoload for all of our classes. Without autoloading, you will have to require the desired file before using its functionality. But now we will let PHP do it for us.
Symfony2 follows de facto standards for PHP, in particular
PSR-0 , for naming classes and autoloaders. The Symfony2 ClassLoader component provides the PSR-0 standard and in most cases all you need is to download this component.
Create an empty startup manager in the file: autoload.php:
<?php
Now you can execute this file using the console, it does not perform any actions and should not give an error (s):
$ php autoload.php
- You can learn more about the ClassLoader component on the Symfony2 website.
Composer automatically creates an autoloader for all necessary components. You can simply include the vendor / .composer / autoload.php file (instead of the autoload.php file)
Our project
Instead of creating a framework with a template, we will write the simplest application again and again, adding one level of abstraction each time. Let's start with the simplest web application in PHP:
<?php $input = $_GET['name']; printf('Hello %s', $input);
The first part ends here. In the next part, we will look at the HttpFoundation component and what functionality it provides to us.
Special thanks to user 1nf for help with translation.