Skip to content

Commit 55f5c12

Browse files
committed
updated the whole book (changes mainly related to Composer)
1 parent 842e4d1 commit 55f5c12

File tree

8 files changed

+77
-117
lines changed

8 files changed

+77
-117
lines changed

book/part01.rst

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -94,44 +94,23 @@ To store our framework, create a directory somewhere on your machine:
9494
$ mkdir framework
9595
$ cd framework
9696
97-
Components Installation
98-
~~~~~~~~~~~~~~~~~~~~~~~
97+
Dependency Management
98+
~~~~~~~~~~~~~~~~~~~~~
9999

100-
To install the Symfony2 Components that we need for our framework, we are
101-
going to use `Composer`_, a project dependency manager for PHP. Create a
102-
``composer.json`` file, where we will list our dependencies:
103-
104-
.. code-block:: javascript
105-
106-
{
107-
"require": {
108-
}
109-
}
110-
111-
The file is empty for now as we do not depend on anything yet. To install the
112-
project dependencies, download the composer binary and run it:
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:
113103

114104
.. code-block:: sh
115105
116-
$ wget http://getcomposer.org/composer.phar
117-
$ # or
118-
$ curl -O http://getcomposer.org/composer.phar
119-
120-
$ php composer.phar install
121-
122-
After running the ``install`` command, you must see a new ``vendor/``
123-
directory.
106+
$ curl -sS https://getcomposer.org/installer | php
124107
125-
Naming Conventions and Autoloading
126-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
108+
Then, generate an empty ``composer.json`` file, where Composer will store the
109+
framework dependencies:
127110

128-
We are going to `autoload`_ all our classes. Without autoloading, you need to
129-
require the file where a class is defined before being able to use it. But
130-
with some conventions, we can just let PHP do the hard work for us.
111+
.. code-block:: sh
131112
132-
Symfony2 follows the de-facto PHP standard, `PSR-0`_, for class names and
133-
autoloading and Composer generates such an autoloader for all the dependencies
134-
it manages; it can be enabled by requiring the ``vendor/autoload.php`` file.
113+
$ php composer.phar init -n
135114
136115
Our Project
137116
-----------
@@ -148,12 +127,18 @@ start with the simplest web application we can think of in PHP::
148127

149128
printf('Hello %s', $input);
150129

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+
151137
In the next chapter, we are going to introduce the HttpFoundation Component
152138
and see what it brings us.
153139

154-
.. _`Symfony2`: http://symfony.com/
155-
.. _`documentation`: http://symfony.com/doc
156-
.. _`Silex`: http://silex.sensiolabs.org/
157-
.. _`autoload`: http://fr.php.net/autoload
158-
.. _`Composer`: http://packagist.org/about-composer
159-
.. _`PSR-0`: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
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

book/part02.rst

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ it suffers from a few problems::
2626

2727
printf('Hello %s', $input);
2828

29-
First, if the ``name`` query parameter is not given in the URL query string,
29+
First, if the ``name`` query parameter is not defined in the URL query string,
3030
you will get a PHP warning; so let's fix it::
3131

3232
<?php
@@ -122,22 +122,22 @@ approach; that's the main goal of the Symfony2 HttpFoundation component:
122122
replacing the default PHP global variables and functions by an Object-Oriented
123123
layer.
124124

125-
To use this component, open the ``composer.json`` file and add it as a
126-
dependency for the project:
125+
To use this component, add it as a dependency of the project:
127126

128-
.. code-block:: javascript
127+
.. code-block:: sh
129128
130-
{
131-
"require": {
132-
"symfony/http-foundation": "~2.3"
133-
}
134-
}
129+
$ php composer.phar require symfony/http-foundation 2.5.*
135130
136-
Then, run the composer ``update`` command:
131+
Running this command will also automatically download the Symfony
132+
HttpFoundation component and install it under the ``vendor/`` directory.
137133

138-
.. code-block:: sh
134+
.. sidebar:: Class Autoloading
139135

140-
$ php composer.phar update
136+
When installing a new dependency, Composer also generates a
137+
``vendor/autoload.php`` file that allows any class to be easily
138+
`autoloaded`_. Without autoloading, you would need to require the file
139+
where a class is defined before being able to use it. But thanks to
140+
`PSR-0`_, we can just let Composer and PHP do the hard work for us.
141141

142142
Now, let's rewrite our application by using the ``Request`` and the
143143
``Response`` classes::
@@ -309,8 +309,8 @@ the wheel.
309309

310310
I've almost forgot to talk about one added benefit: using the HttpFoundation
311311
component is the start of better interoperability between all frameworks and
312-
applications using it (as of today `Symfony2`_, `Drupal 8`_, `phpBB 4`_,
313-
`ezPublish 5`, `Silex`_, `Midgard CMS`_, `Zikula`_ ...).
312+
applications using it (like `Symfony2`_, `Drupal 8`_, `phpBB 4`_, `ezPublish
313+
5`, `Laravel`_, `Silex`_, and `more`_).
314314

315315
.. _`Twig`: http://twig.sensiolabs.com/
316316
.. _`Symfony2 versus Flat PHP`: http://symfony.com/doc/current/book/from_flat_php_to_symfony2.html
@@ -324,3 +324,6 @@ applications using it (as of today `Symfony2`_, `Drupal 8`_, `phpBB 4`_,
324324
.. _`Silex`: http://silex.sensiolabs.org/
325325
.. _`Midgard CMS`: http://www.midgard-project.org/
326326
.. _`Zikula`: http://zikula.org/
327+
.. _`autoloaded`: http://php.net/autoload
328+
.. _`PSR-0`: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
329+
.. _`more`: http://symfony.com/components/HttpFoundation

book/part03.rst

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ And for the "Goodbye" page::
6464
$response->send();
6565

6666
We have indeed moved most of the shared code into a central place, but it does
67-
not feel like a good abstraction, doesn't it? First, we still have the
68-
``send()`` method in all pages, then our pages do not look like templates, and
69-
we are still not able to test this code properly.
67+
not feel like a good abstraction, does it? We still have the ``send()`` method
68+
for all pages, our pages do not look like templates, and we are still not able
69+
to test this code properly.
7070

7171
Moreover, adding a new page means that we need to create a new PHP script,
7272
which name is exposed to the end user via the URL
73-
(``http://example.com/bye.php``): there is a direct mapping between the PHP
73+
(``http://127.0.0.1:4321/bye.php``): there is a direct mapping between the PHP
7474
script name and the client URL. This is because the dispatching of the request
7575
is done by the web server directly. It might be a good idea to move this
7676
dispatching to our code for better flexibility. This can be easily achieved by
@@ -127,18 +127,17 @@ we return a custom 404 page; you are now in control of your website.
127127

128128
To access a page, you must now use the ``front.php`` script:
129129

130-
* ``http://example.com/front.php/hello?name=Fabien``
130+
* ``http://127.0.0.1:4321/front.php/hello?name=Fabien``
131131

132-
* ``http://example.com/front.php/bye``
132+
* ``http://127.0.0.1:4321/front.php/bye``
133133

134134
``/hello`` and ``/bye`` are the page *path*s.
135135
136136
.. tip::
137137

138-
Most web servers like Apache or nginx are able to rewrite the incoming
139-
URLs and remove the front controller script so that your users will be
140-
able to type ``http://example.com/hello?name=Fabien``, which looks much
141-
better.
138+
Most web servers like Apache or nginx are able to rewrite the incoming URLs
139+
and remove the front controller script so that your users will be able to
140+
type ``http://127.0.0.1:4321/hello?name=Fabien``, which looks much better.
142141

143142
The trick is the usage of the ``Request::getPathInfo()`` method which returns
144143
the path of the Request by removing the front controller script name including
@@ -152,8 +151,8 @@ its sub-directories (only if needed -- see above tip).
152151
argument is the URL path you want to simulate.
153152

154153
Now that the web server always access the same script (``front.php``) for all
155-
our pages, we can secure our code further by moving all other PHP files
156-
outside the web root directory:
154+
pages, we can secure the code further by moving all other PHP files outside the
155+
web root directory:
157156

158157
.. code-block:: text
159158
@@ -170,14 +169,21 @@ outside the web root directory:
170169
Now, configure your web server root directory to point to ``web/`` and all
171170
other files won't be accessible from the client anymore.
172171

172+
To test your changes in a browser (``http://localhost:4321/?name=Fabien``), run
173+
the PHP built-in server:
174+
175+
.. code-block:: sh
176+
177+
$ php -S 127.0.0.1:4321 -t web/ web/front.php
178+
173179
.. note::
174180

175181
For this new structure to work, you will have to adjust some paths in
176182
various PHP files; the changes are left as an exercise for the reader.
177183

178184
The last thing that is repeated in each page is the call to ``setContent()``.
179-
We can convert all pages to "templates" by just echoing the content and
180-
calling the ``setContent()`` directly from the front controller script::
185+
We can convert all pages to "templates" by just echoing the content and calling
186+
the ``setContent()`` directly from the front controller script::
181187

182188
<?php
183189

book/part04.rst

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,11 @@ instead of relying on a query string:
5353
# After
5454
/hello/Fabien
5555

56-
To support this feature, we are going to use the Symfony2 Routing component.
57-
As always, add it to ``composer.json`` and run the ``php composer.phar
58-
update`` command to install it:
56+
To support this feature, add the Symfony2 Routing component as a dependency:
5957

60-
.. code-block:: javascript
58+
.. code-block:: sh
6159
62-
{
63-
"require": {
64-
"symfony/http-foundation": "~2.3",
65-
"symfony/routing": "~2.3"
66-
}
67-
}
60+
$ php composer.phar require symfony/routing 2.5.*
6861
6962
Instead of an array for the URL map, the Routing component relies on a
7063
``RouteCollection`` instance::

book/part06.rst

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,11 @@ lazy-loaded so that only the controller associated with the matched route is
3737
instantiated.
3838

3939
To solve this issue, and a bunch more, let's install and use the HttpKernel
40-
component::
40+
component:
4141

42-
{
43-
"require": {
44-
"symfony/http-foundation": "~2.3",
45-
"symfony/routing": "~2.3",
46-
"symfony/http-kernel": "~2.3"
47-
}
48-
}
42+
.. code-block:: sh
43+
44+
$ php composer.phar require symfony/http-kernel 2.5.*
4945
5046
The HttpKernel component has many interesting features, but the one we need
5147
right now is the *controller resolver*. A controller resolver knows how to

book/part07.rst

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ Request, and one output, the Response. Our framework class will follow this
1212
simple principle: the logic is about creating the Response associated with a
1313
Request.
1414

15-
As the Symfony2 components requires PHP 5.3, let's create our very own
16-
namespace for our framework: ``Simplex``.
17-
18-
Move the request handling logic into its own ``Simplex\\Framework`` class::
15+
Let's create our very own namespace for our framework: ``Simplex``. Move the
16+
request handling logic into its own ``Simplex\\Framework`` class::
1917

2018
<?php
2119

@@ -89,9 +87,9 @@ be autoloaded, update the ``composer.json`` file:
8987
9088
{
9189
"require": {
92-
"symfony/http-foundation": "~2.3",
93-
"symfony/routing": "~2.3",
94-
"symfony/http-kernel": "~2.3"
90+
"symfony/http-foundation": "2.5.*",
91+
"symfony/routing": "2.5.*",
92+
"symfony/http-kernel": "2.5.*"
9593
},
9694
"autoload": {
9795
"psr-0": { "Simplex\\": "src/", "Calendar\\": "src/" }
@@ -100,7 +98,7 @@ be autoloaded, update the ``composer.json`` file:
10098
10199
.. note::
102100

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

105103
Move the controller to ``Calendar\\Controller\\LeapYearController``::
106104

book/part09.rst

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +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:: javascript
20+
.. code-block:: sh
2121
22-
{
23-
"require": {
24-
"symfony/http-foundation": "~2.3",
25-
"symfony/routing": "~2.3",
26-
"symfony/http-kernel": "~2.3",
27-
"symfony/event-dispatcher": "~2.3"
28-
},
29-
"autoload": {
30-
"psr-0": { "Simplex\\": "src/", "Calendar\\": "src/" }
31-
}
32-
}
22+
$ php composer.phar require symfony/event-dispatcher 2.5.*
3323
3424
How does it work? The *dispatcher*, the central object of the event dispatcher
3525
system, notifies *listeners* of an *event* dispatched to it. Put another way:

book/part12.rst

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +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:: javascript
94+
.. code-block:: sh
9595
96-
{
97-
"require": {
98-
"symfony/http-foundation": "~2.3",
99-
"symfony/routing": "~2.3",
100-
"symfony/http-kernel": "~2.3",
101-
"symfony/event-dispatcher": "~2.3",
102-
"symfony/dependency-injection": "~2.3"
103-
},
104-
"autoload": {
105-
"psr-0": { "Simplex\\": "src/", "Calendar\\": "src/" }
106-
}
107-
}
96+
$ php composer.phar require symfony/dependency-injection 2.5.*
10897
10998
Create a new file to host the dependency injection container configuration::
11099

@@ -244,7 +233,7 @@ definitions, from scope support to dumping a container to a plain PHP class,
244233
and much more. The Symfony dependency injection container is really powerful
245234
and is able to manage any kind of PHP class.
246235

247-
Don't yell at me if you don't want to have a dependency injection container in
236+
Don't yell at me if you don't want to use a dependency injection container in
248237
your framework. If you don't like it, don't use it. It's your framework, not
249238
mine.
250239

0 commit comments

Comments
 (0)