|
| 1 | +Create your own framework... on top of the Symfony2 Components (part 1) |
| 2 | +======================================================================= |
| 3 | + |
| 4 | +Symfony2 is a reusable set of standalone, decoupled, and cohesive PHP |
| 5 | +components that solve common web development problems. |
| 6 | + |
| 7 | +Instead of using these low-level components, you can use the ready-to-be-used |
| 8 | +Symfony2 full-stack web framework, which is based on these components... or |
| 9 | +you can create your very own framework. This series is about the latter. |
| 10 | + |
| 11 | +.. note:: |
| 12 | + |
| 13 | + If you just want to use the Symfony2 full-stack framework, you'd better |
| 14 | + read its official `documentation`_ instead. |
| 15 | + |
| 16 | +Why would you like to create your own framework? |
| 17 | +------------------------------------------------ |
| 18 | + |
| 19 | +Why would you like to create your own framework in the first place? If you |
| 20 | +look around, everybody will tell you that it's a bad thing to reinvent the |
| 21 | +wheel and that you'd better choose an existing framework and forget about |
| 22 | +creating your own altogether. Most of the time, they are right but I can think |
| 23 | +of a few good reasons to start creating your own framework: |
| 24 | + |
| 25 | +* To learn more about the low level architecture of modern web frameworks in |
| 26 | + general and about the Symfony2 full-stack framework internals in particular; |
| 27 | + |
| 28 | +* To create a framework tailored to your very specific needs (just be sure |
| 29 | + first that your needs are really specific); |
| 30 | + |
| 31 | +* To experiment creating a framework for fun (in a learn-and-throw-away |
| 32 | + approach); |
| 33 | + |
| 34 | +* To refactor an old/existing application that needs a good dose of recent web |
| 35 | + development best practices; |
| 36 | + |
| 37 | +* To prove the world that you can actually create a framework on your own (... |
| 38 | + but with little effort). |
| 39 | + |
| 40 | +I will gently guide you through the creation of a web framework, one step at a |
| 41 | +time. At each step, you will have a fully-working framework that you can use |
| 42 | +as is or as a start for your very own. We will start with simple frameworks |
| 43 | +and more features will be added with time. Eventually, you will have a |
| 44 | +fully-featured full-stack web framework. |
| 45 | + |
| 46 | +And of course, each step will be the occasion to learn more about some of the |
| 47 | +Symfony2 Components. |
| 48 | + |
| 49 | +.. tip:: |
| 50 | + |
| 51 | + If you don't have time to read the whole series, or if you want to get |
| 52 | + started fast, you can also have a look at `Silex`_, a micro-framework |
| 53 | + based on the Symfony2 Components. The code is rather slim and it leverages |
| 54 | + many aspects of the Symfony2 Components. |
| 55 | + |
| 56 | +Many modern web frameworks call themselves MVC frameworks. We won't talk about |
| 57 | +MVC here as the Symfony2 Components are able to create any type of frameworks, |
| 58 | +not just the ones that follow the MVC architecture. Anyway, if you have a look |
| 59 | +at the MVC semantics, this series is about how to create the Controller part |
| 60 | +of a framework. For the Model and the View, it really depends on your personal |
| 61 | +taste and I will let you use any existing third-party libraries (Doctrine, |
| 62 | +Propel, or plain-old PDO for the Model; PHP or Twig for the View). |
| 63 | + |
| 64 | +When creating a framework, following the MVC pattern is not the right goal. |
| 65 | +The main goal should be the Separation of Concerns; I actually think that this |
| 66 | +is the only design pattern that you should really care about. The fundamental |
| 67 | +principles of the Symfony2 Components are centered around the HTTP |
| 68 | +specification. As such, the frameworks that we are going to create should be |
| 69 | +more accurately labelled as HTTP frameworks or Request/Response frameworks. |
| 70 | + |
| 71 | +Before we start |
| 72 | +--------------- |
| 73 | + |
| 74 | +Reading about how to create a framework is not enough. You will have to follow |
| 75 | +along and actually type all the examples we will work on. For that, you need a |
| 76 | +recent version of PHP (5.3.8 or later is good enough), a web server (like |
| 77 | +Apache or NGinx), a good knowledge of PHP and an understanding of Object |
| 78 | +Oriented programming. |
| 79 | + |
| 80 | +Ready to go? Let's start. |
| 81 | + |
| 82 | +Bootstrapping |
| 83 | +------------- |
| 84 | + |
| 85 | +Before we can even think of creating our first framework, we need to talk |
| 86 | +about some conventions: where we will store our code, how we will name our |
| 87 | +classes, how we will reference external dependencies, etc. |
| 88 | + |
| 89 | +To store our framework, create a directory somewhere on your machine: |
| 90 | + |
| 91 | +.. code-block: sh |
| 92 | +
|
| 93 | + $ mkdir framework |
| 94 | + $ cd framework |
| 95 | +
|
| 96 | +Coding Standards |
| 97 | +~~~~~~~~~~~~~~~~ |
| 98 | + |
| 99 | +Before anyone starts a flame war about coding standards and why the one used |
| 100 | +here suck hard, let's all admit that this does not matter that much as long as |
| 101 | +you are consistent. For this book, we are going to use the `Symfony2 Coding |
| 102 | +Standards`_. |
| 103 | + |
| 104 | +Components Installation |
| 105 | +~~~~~~~~~~~~~~~~~~~~~~~ |
| 106 | + |
| 107 | +To install the Symfony2 Components that we need for our framework, we are |
| 108 | +going to use `Composer`_, a project dependency manager for PHP. First, list |
| 109 | +your dependencies in a ``composer.json`` file: |
| 110 | + |
| 111 | +.. code-block:: json |
| 112 | +
|
| 113 | + # framework/composer.json |
| 114 | + { |
| 115 | + "require": { |
| 116 | + "symfony/class-loader": "2.1.*" |
| 117 | + } |
| 118 | + } |
| 119 | +
|
| 120 | +Here, we tell Composer that our project depends on the Symfony2 ClassLoader |
| 121 | +component, version 2.1.0 or later. To actually install the project |
| 122 | +dependencies, download the composer binary and run it: |
| 123 | + |
| 124 | +.. code-block:: sh |
| 125 | +
|
| 126 | + $ wget http://getcomposer.org/composer.phar |
| 127 | + $ # or |
| 128 | + $ curl -O http://getcomposer.org/composer.phar |
| 129 | +
|
| 130 | + $ php composer.phar install |
| 131 | +
|
| 132 | +After running the ``install`` command, you must see a new ``vendor/`` |
| 133 | +directory that must contain the Symfony2 ClassLoader code. |
| 134 | + |
| 135 | +.. note:: |
| 136 | + |
| 137 | + Even if we highly recommend you the use of Composer, you can also download |
| 138 | + the archives of the components directly or use Git submodules. That's |
| 139 | + really up to you. |
| 140 | + |
| 141 | +Naming Conventions and Autoloading |
| 142 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 143 | + |
| 144 | +We are going to `autoload`_ all our classes. Without autoloading, you need to |
| 145 | +require the file where a class is defined before being able to use it. But |
| 146 | +with some conventions, we can just let PHP do the hard work for us. |
| 147 | + |
| 148 | +Symfony2 follows the de-facto PHP standard, `PSR-0`_, for class names and |
| 149 | +autoloading. The Symfony2 ClassLoader Component provides an autoloader that |
| 150 | +implements this PSR-0 standard and most of the time, the Symfony2 ClassLoader |
| 151 | +is all you need to autoload all your project classes. |
| 152 | + |
| 153 | +Create and empty autoloader in a new ``autoload.php`` file: |
| 154 | + |
| 155 | +.. code-block:: php |
| 156 | +
|
| 157 | + <?php |
| 158 | +
|
| 159 | + // framework/autoload.php |
| 160 | +
|
| 161 | + require_once __DIR__.'/vendor/symfony/class-loader/Symfony/Component/ClassLoader/UniversalClassLoader.php'; |
| 162 | +
|
| 163 | + use Symfony\Component\ClassLoader\UniversalClassLoader; |
| 164 | +
|
| 165 | + $loader = new UniversalClassLoader(); |
| 166 | + $loader->register(); |
| 167 | +
|
| 168 | +You can now run the ``autoload.php`` on the CLI, it should not do anything and |
| 169 | +should not throw any error: |
| 170 | + |
| 171 | +.. code-block:: sh |
| 172 | +
|
| 173 | + $ php autoload.php |
| 174 | +
|
| 175 | +.. tip:: |
| 176 | + |
| 177 | + The Symfony website has more information about the `ClassLoader`_ |
| 178 | + component. |
| 179 | + |
| 180 | +Our Project |
| 181 | +----------- |
| 182 | + |
| 183 | +Instead of creating our framework from scratch, we are going to write the same |
| 184 | +"application" over and over again, adding one abstraction at a time. Let's |
| 185 | +start with the simplest web application we can think of in PHP:: |
| 186 | + |
| 187 | + <?php |
| 188 | + |
| 189 | + $input = $_GET['name']; |
| 190 | + |
| 191 | + printf('Hello %s', $_GET['name']); |
| 192 | + |
| 193 | +That's all for the first part of this series. Next time, we will introduce the |
| 194 | +HttpFoundation Component and see what it will brings us. |
| 195 | + |
| 196 | +.. _`documentation`: http://symfony.com/doc |
| 197 | +.. _`Silex`: http://silex.sensiolabs.org/ |
| 198 | +.. _`autoload`: http://fr.php.net/autoload |
| 199 | +.. _`Composer`: http://packagist.org/about-composer |
| 200 | +.. _`PSR-0`: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md |
| 201 | +.. _`Symfony2 Coding Standards`: http://symfony.com/doc/current/contributing/code/standards.html |
| 202 | +.. _`ClassLoader`: http://symfony.com/doc/current/components/class_loader.html |
0 commit comments