We create our own framework based on Symfony2. (Part 1)

Original author: Fabien Potencier
  • Transfer
Table of contents

Symfony2 is a set of standalone, reusable libraries, not tied to each other, that solve common tasks in web development.

Instead of using low-level components, you can use a full-scale framework that is based on these components - Symfony2 ... Or, you can write your own. This series of articles about 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, but use a ready-made solution. And in most cases they will be right, but there are several reasons to start developing your framework:

  1. To explore the low-level architecture of modern web-based frameworks, in particular Symfony2 and its components.
  2. To create a turnkey framework for your specific needs. (Just make sure your needs are really specific)
  3. For an experiment for fun (learned and scored)
  4. For refactoring existing applications that need more modern and well-established practical solutions.
  5. To prove that you can do it (... albeit with a little help)

I will briefly, step by step, lead you through the topic of creating your own framework. At each step you will have a fully functional 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 over time your framework will become more functional.
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 don’t have time to read the full series of articles, or want to start development now, turn your 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 you can create a framework not based on this model based on Symfony2 components. In any case, if you look at the construction of MVC, this part will be about how to create a part of the Contoller 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 the good old PDO for Model and / or PHP, Twig for View).

When creating the framework, the main goal should be - separation into components, rather than blindly following the MVC pattern. The fundamental principles of 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’s not enough to just read about creating a framework. You really need to write the code for the examples we are looking at. For this you need PHP (version 5.3.8 or newer), a web server (Apache or Nginx), good knowledge in PHP and OOP.

Are you ready? Then let's start.


Before we start thinking about our framework, we need to stipulate several conventions: where we will store the code, how we will call our classes, how we will connect / process external dependencies.

To place the framework, create a directory somewhere on your server:
mkdir framework
cd framework

Coding Standards

Before someone starts a holivor about this, let's admit that for example, it does not matter what standards to use. Therefore, in the series of these articles we will use the "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 will 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 the project dependencies, do the following:
$ wget http://getcomposer.org/composer.phar
$ # or
$ curl -O http://getcomposer.org/composer.phar
$ php composer.phar install

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 the archived component or use the Git submodules. You decide.

Naming Conventions and Autoloading

We will apply autoload to all of our classes. Without startup, you will have to do require the desired file before using its functionality. But for now, we will let PHP do this for us.
Symfony2 follows de facto standards for PHP, in particular PSR-0 , for class naming and autoloader. The Symfony2 ClassLoader component provides the PSR-0 standard and in most cases all you need to do is download this component.
Create an empty startup manager in the file: autoload.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 from 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:

На этом первая часть заканчивается. В следующей части мы рассмотрим компонент HttpFoundation и какой функционал он нам предоставляет.

Отдельное спасибо юзеру 1nf за помощь в переводе.

Also popular now: