|
| 1 | +Introduction |
| 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 book 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 book, 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 advertize themselves as being MVC frameworks. We |
| 57 | +won't talk about the MVC pattern as the Symfony2 Components are able to create |
| 58 | +any type of frameworks, not just the ones that follow the MVC architecture. |
| 59 | +Anyway, if you have a look at the MVC semantics, this book is about how to |
| 60 | +create the Controller part of a framework. For the Model and the View, it |
| 61 | +really depends on your personal taste and I will let you use any existing |
| 62 | +third-party libraries (Doctrine, Propel, or plain-old PDO for the Model; PHP |
| 63 | +or Twig for the View). |
| 64 | + |
| 65 | +When creating a framework, following the MVC pattern is not the right goal. |
| 66 | +The main goal should be the **Separation of Concerns**; I actually think that |
| 67 | +this is the only design pattern that you should really care about. The |
| 68 | +fundamental principles of the Symfony2 Components are focused on the HTTP |
| 69 | +specification. As such, the frameworks that we are going to create should be |
| 70 | +more accurately labelled as HTTP frameworks or Request/Response frameworks. |
| 71 | + |
| 72 | +Before we start |
| 73 | +--------------- |
| 74 | + |
| 75 | +Reading about how to create a framework is not enough. You will have to follow |
| 76 | +along and actually type all the examples we will work on. For that, you need a |
| 77 | +recent version of PHP (5.3.8 or later is good enough), a web server (like |
| 78 | +Apache or NGinx), a good knowledge of PHP and an understanding of Object |
| 79 | +Oriented programming. |
| 80 | + |
| 81 | +Ready to go? Let's start. |
| 82 | + |
| 83 | +Bootstrapping |
| 84 | +------------- |
| 85 | + |
| 86 | +Before we can even think of creating our first framework, we need to talk |
| 87 | +about some conventions: where we will store our code, how we will name our |
| 88 | +classes, how we will reference external dependencies, etc. |
| 89 | + |
| 90 | +To store our framework, create a directory somewhere on your machine: |
| 91 | + |
| 92 | +.. code-block:: sh |
| 93 | +
|
| 94 | + $ mkdir framework |
| 95 | + $ cd framework |
| 96 | +
|
| 97 | +Dependency Management |
| 98 | +~~~~~~~~~~~~~~~~~~~~~ |
| 99 | + |
| 100 | +To install the Symfony2 Components that we need for our framework, we are going |
| 101 | +to use `Composer`_, a project dependency manager for PHP. If you don't have it |
| 102 | +yet, `download and install`_ Composer now: |
| 103 | + |
| 104 | +.. code-block:: sh |
| 105 | +
|
| 106 | + $ curl -sS https://getcomposer.org/installer | php |
| 107 | +
|
| 108 | +Then, generate an empty ``composer.json`` file, where Composer will store the |
| 109 | +framework dependencies: |
| 110 | + |
| 111 | +.. code-block:: sh |
| 112 | +
|
| 113 | + $ php composer.phar init -n |
| 114 | +
|
| 115 | +Our Project |
| 116 | +----------- |
| 117 | + |
| 118 | +Instead of creating our framework from scratch, we are going to write the same |
| 119 | +"application" over and over again, adding one abstraction at a time. Let's |
| 120 | +start with the simplest web application we can think of in PHP:: |
| 121 | + |
| 122 | + <?php |
| 123 | + |
| 124 | + // framework/index.php |
| 125 | + |
| 126 | + $input = $_GET['name']; |
| 127 | + |
| 128 | + printf('Hello %s', $input); |
| 129 | + |
| 130 | +Use the PHP built-in server to test this great application in a browser |
| 131 | +(``http://localhost:4321/index.php?name=Fabien``): |
| 132 | + |
| 133 | +.. code-block:: sh |
| 134 | +
|
| 135 | + $ php -S 127.0.0.1:4321 |
| 136 | +
|
| 137 | +In the next chapter, we are going to introduce the HttpFoundation Component |
| 138 | +and see what it brings us. |
| 139 | + |
| 140 | +.. _`Symfony2`: http://symfony.com/ |
| 141 | +.. _`documentation`: http://symfony.com/doc |
| 142 | +.. _`Silex`: http://silex.sensiolabs.org/ |
| 143 | +.. _`Composer`: http://packagist.org/about-composer |
| 144 | +.. _`download and install`: https://getcomposer.org/doc/01-basic-usage.md |
0 commit comments