Skip to content

Commit 13a7170

Browse files
committed
made some changes to better integrate the tutorial into the current documentation
1 parent bf9c871 commit 13a7170

10 files changed

+52
-62
lines changed

create_framework/01-introduction.rst

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,14 @@ Instead of using these low-level components, you can use the ready-to-be-used
88
Symfony2 full-stack web framework, which is based on these components... or
99
you can create your very own framework. This book is about the latter.
1010

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-
1611
Why would you like to create your own framework?
1712
------------------------------------------------
1813

1914
Why would you like to create your own framework in the first place? If you
2015
look around, everybody will tell you that it's a bad thing to reinvent the
2116
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:
17+
creating your own altogether. Most of the time, they are right but there are
18+
a few good reasons to start creating your own framework:
2419

2520
* To learn more about the low level architecture of modern web frameworks in
2621
general and about the Symfony2 full-stack framework internals in particular;
@@ -37,11 +32,11 @@ of a few good reasons to start creating your own framework:
3732
* To prove the world that you can actually create a framework on your own (...
3833
but with little effort).
3934

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.
35+
This tutorial will gently guide you through the creation of a web framework,
36+
one step at a time. At each step, you will have a fully-working framework that
37+
you can use as is or as a start for your very own. We will start with simple
38+
frameworks and more features will be added with time. Eventually, you will have
39+
a fully-featured full-stack web framework.
4540

4641
And of course, each step will be the occasion to learn more about some of the
4742
Symfony2 Components.
@@ -58,16 +53,16 @@ won't talk about the MVC pattern as the Symfony2 Components are able to create
5853
any type of frameworks, not just the ones that follow the MVC architecture.
5954
Anyway, if you have a look at the MVC semantics, this book is about how to
6055
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
56+
really depends on your personal taste and you can use any existing
6257
third-party libraries (Doctrine, Propel, or plain-old PDO for the Model; PHP
6358
or Twig for the View).
6459

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.
60+
When creating a framework, following the MVC pattern is not the right goal. The
61+
main goal should be the **Separation of Concerns**; this is probably the only
62+
design pattern that you should really care about. The fundamental principles of
63+
the Symfony2 Components are focused on the HTTP specification. As such, the
64+
frameworks that we are going to create should be more accurately labelled as
65+
HTTP frameworks or Request/Response frameworks.
7166

7267
Before we start
7368
---------------
@@ -89,7 +84,7 @@ classes, how we will reference external dependencies, etc.
8984

9085
To store our framework, create a directory somewhere on your machine:
9186

92-
.. code-block:: sh
87+
.. code-block:: bash
9388
9489
$ mkdir framework
9590
$ cd framework
@@ -101,16 +96,16 @@ To install the Symfony2 Components that we need for our framework, we are going
10196
to use `Composer`_, a project dependency manager for PHP. If you don't have it
10297
yet, `download and install`_ Composer now:
10398

104-
.. code-block:: sh
99+
.. code-block:: bash
105100
106101
$ curl -sS https://getcomposer.org/installer | php
107102
108103
Then, generate an empty ``composer.json`` file, where Composer will store the
109104
framework dependencies:
110105

111-
.. code-block:: sh
106+
.. code-block:: bash
112107
113-
$ php composer.phar init -n
108+
$ composer init -n
114109
115110
Our Project
116111
-----------
@@ -130,7 +125,7 @@ start with the simplest web application we can think of in PHP::
130125
Use the PHP built-in server to test this great application in a browser
131126
(``http://localhost:4321/index.php?name=Fabien``):
132127

133-
.. code-block:: sh
128+
.. code-block:: bash
134129
135130
$ php -S 127.0.0.1:4321
136131

create_framework/02-http-foundation.rst

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
The HttpFoundation Component
22
============================
33

4-
Before diving into the framework creation process, I first want to step back
5-
and take a look at why you would like to use a framework instead of keeping
6-
your plain-old PHP applications as is. Why using a framework is actually a
7-
good idea, even for the simplest snippet of code and why creating your
8-
framework on top of the Symfony2 components is better than creating a
9-
framework from scratch.
4+
Before diving into the framework creation process, let's first step back and
5+
let's take a look at why you would like to use a framework instead of keeping
6+
your plain-old PHP applications as is. Why using a framework is actually a good
7+
idea, even for the simplest snippet of code and why creating your framework on
8+
top of the Symfony2 components is better than creating a framework from scratch.
109

1110
.. note::
1211

13-
I won't talk about the obvious and traditional benefits of using a
12+
We won't talk about the obvious and traditional benefits of using a
1413
framework when working on big applications with more than a few
1514
developers; the Internet has already plenty of good resources on that
1615
topic.
@@ -124,9 +123,9 @@ layer.
124123

125124
To use this component, add it as a dependency of the project:
126125

127-
.. code-block:: sh
126+
.. code-block:: bash
128127
129-
$ php composer.phar require symfony/http-foundation
128+
$ composer require symfony/http-foundation
130129
131130
Running this command will also automatically download the Symfony
132131
HttpFoundation component and install it under the ``vendor/`` directory.
@@ -270,13 +269,12 @@ chained proxies)::
270269
// the client is a known one, so give it some more privilege
271270
}
272271

273-
And there is an added benefit: it is *secure* by default. What do I mean by
274-
secure? The ``$_SERVER['HTTP_X_FORWARDED_FOR']`` value cannot be trusted as it
275-
can be manipulated by the end user when there is no proxy. So, if you are
276-
using this code in production without a proxy, it becomes trivially easy to
277-
abuse your system. That's not the case with the ``getClientIp()`` method as
278-
you must explicitly trust your reverse proxies by calling
279-
``setTrustedProxies()``::
272+
And there is an added benefit: it is *secure* by default. What does it mean?
273+
The ``$_SERVER['HTTP_X_FORWARDED_FOR']`` value cannot be trusted as it can be
274+
manipulated by the end user when there is no proxy. So, if you are using this
275+
code in production without a proxy, it becomes trivially easy to abuse your
276+
system. That's not the case with the ``getClientIp()`` method as you must
277+
explicitly trust your reverse proxies by calling ``setTrustedProxies()``::
280278

281279
<?php
282280

@@ -295,8 +293,8 @@ cases by yourself. Why not using a technology that already works?
295293
.. note::
296294

297295
If you want to learn more about the HttpFoundation component, you can have
298-
a look at the `API`_ or read its dedicated `documentation`_ on the Symfony
299-
website.
296+
a look at the :namespace:`Symfony\\Component\\HttpFoundation` API or read
297+
its dedicated :doc:`documentation </components/http_foundation/index>`.
300298

301299
Believe or not but we have our first framework. You can stop now if you want.
302300
Using just the Symfony2 HttpFoundation component already allows you to write
@@ -315,8 +313,6 @@ applications using it (like `Symfony2`_, `Drupal 8`_, `phpBB 4`_, `ezPublish
315313
.. _`Twig`: http://twig.sensiolabs.com/
316314
.. _`Symfony2 versus Flat PHP`: http://symfony.com/doc/current/book/from_flat_php_to_symfony2.html
317315
.. _`HTTP specification`: http://tools.ietf.org/wg/httpbis/
318-
.. _`API`: http://api.symfony.com/2.0/Symfony/Component/HttpFoundation.html
319-
.. _`documentation`: http://symfony.com/doc/current/components/http_foundation.html
320316
.. _`audited`: http://symfony.com/blog/symfony2-security-audit
321317
.. _`Symfony2`: http://symfony.com/
322318
.. _`Drupal 8`: http://drupal.org/

create_framework/03-front-controller.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ other files won't be accessible from the client anymore.
172172
To test your changes in a browser (``http://localhost:4321/?name=Fabien``), run
173173
the PHP built-in server:
174174

175-
.. code-block:: sh
175+
.. code-block:: bash
176176
177177
$ php -S 127.0.0.1:4321 -t web/ web/front.php
178178

create_framework/04-routing.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ instead of relying on a query string:
5555

5656
To support this feature, add the Symfony2 Routing component as a dependency:
5757

58-
.. code-block:: sh
58+
.. code-block:: bash
5959
60-
$ php composer.phar require symfony/routing
60+
$ composer require symfony/routing
6161
6262
Instead of an array for the URL map, the Routing component relies on a
6363
``RouteCollection`` instance::

create_framework/06-http-kernel.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ instantiated.
3939
To solve this issue, and a bunch more, let's install and use the HttpKernel
4040
component:
4141

42-
.. code-block:: sh
42+
.. code-block:: bash
4343
44-
$ php composer.phar require symfony/http-kernel
44+
$ composer require symfony/http-kernel
4545
4646
The HttpKernel component has many interesting features, but the one we need
4747
right now is the *controller resolver*. A controller resolver knows how to

create_framework/07-separation-of-concerns.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ be autoloaded, update the ``composer.json`` file:
9898
9999
.. note::
100100

101-
For the Composer autoloader to be updated, run ``php composer.phar update``.
101+
For the Composer autoloader to be updated, run ``composer update``.
102102

103103
Move the controller to ``Calendar\\Controller\\LeapYearController``::
104104

create_framework/08-unit-testing.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,8 @@ Executing this test is as simple as running ``phpunit`` from the
119119
120120
.. note::
121121

122-
I do not explain how the code works in details as this is not the goal of
123-
this book, but if you don't understand what the hell is going on, I highly
124-
recommend you to read PHPUnit documentation on `test doubles`_.
122+
If you don't understand what the hell is going on in the code, read
123+
PHPUnit documentation on `test doubles`_.
125124

126125
After the test ran, you should see a green bar. If not, you have a bug
127126
either in the test or in the framework code!

create_framework/09-event-dispatcher.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ pattern, the *Observer*, to allow any kind of behaviors to be attached to our
1717
framework; the Symfony2 EventDispatcher Component implements a lightweight
1818
version of this pattern:
1919

20-
.. code-block:: sh
20+
.. code-block:: bash
2121
22-
$ php composer.phar require symfony/event-dispatcher
22+
$ composer require symfony/event-dispatcher
2323
2424
How does it work? The *dispatcher*, the central object of the event dispatcher
2525
system, notifies *listeners* of an *event* dispatched to it. Put another way:
@@ -165,7 +165,7 @@ type is HTML (these conditions demonstrate the ease of manipulating the
165165
Request and Response data from your code).
166166

167167
So far so good, but let's add another listener on the same event. Let's say
168-
that I want to set the ``Content-Length`` of the Response if it is not already
168+
that we want to set the ``Content-Length`` of the Response if it is not already
169169
set::
170170

171171
$dispatcher->addListener('response', function (Simplex\ResponseEvent $event) {

create_framework/10-http-kernel.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ content and check that the number only changes every 10 seconds::
9999
Using HTTP cache headers to manage your application cache is very powerful and
100100
allows you to tune finely your caching strategy as you can use both the
101101
expiration and the validation models of the HTTP specification. If you are not
102-
comfortable with these concepts, I highly recommend you to read the `HTTP
103-
caching`_ chapter of the Symfony2 documentation.
102+
comfortable with these concepts, read the `HTTP caching`_ chapter of the
103+
Symfony2 documentation.
104104

105105
The Response class contains many other methods that let you configure the
106106
HTTP cache very easily. One of the most powerful is ``setCache()`` as it

create_framework/12-dependency-injection.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ front controller? As you might expect, there is a solution. We can solve all
9191
these issues and some more by using the Symfony2 dependency injection
9292
container:
9393

94-
.. code-block:: sh
94+
.. code-block:: bash
9595
96-
$ php composer.phar require symfony/dependency-injection
96+
$ composer require symfony/dependency-injection
9797
9898
Create a new file to host the dependency injection container configuration::
9999

@@ -243,8 +243,8 @@ in great details, but hopefully it gives you enough information to get started
243243
on your own and to better understand how the Symfony2 framework works
244244
internally.
245245

246-
If you want to learn more, I highly recommend you to read the source code of
247-
the `Silex`_ micro-framework, and especially its `Application`_ class.
246+
If you want to learn more, read the source code of the `Silex`_
247+
micro-framework, and especially its `Application`_ class.
248248

249249
Have fun!
250250

0 commit comments

Comments
 (0)