8000 [#5811] Minor fixes to flat PHP to Symfony article · symfony/symfony-docs@9960f9c · GitHub
[go: up one dir, main page]

Skip to content

Commit 9960f9c

Browse files
committed
[#5811] Minor fixes to flat PHP to Symfony article
1 parent fcf82cc commit 9960f9c

File tree

1 file changed

+32
-50
lines changed

1 file changed

+32
-50
lines changed

book/from_flat_php_to_symfony2.rst

Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ persisted to the database. Writing in flat PHP is quick and dirty:
3030
<?php
3131
// index.php
3232
$link = new PDO("mysql:host=localhost;dbname=blog_db", 'myuser', 'mypassword');
33-
33+
3434
$result = $link->query('SELECT id, title FROM post');
35-
$result->setFetchMode(PDO::FETCH_ASSOC);
3635
?>
37-
36+
3837
<!DOCTYPE html>
3938
<html>
4039
<head>
@@ -43,7 +42,7 @@ persisted to the database. Writing in flat PHP is quick and dirty:
4342
<body>
4443
<h1>List of Posts</h1>
4544
<ul>
46-
<?php while ($row = $result->fetch()): ?>
45+
<?php while ($row = $result->fetch(PDO::FETCH_ASSOC)): ?>
4746
<li>
4847
<a href="/show.php?id=<?php echo $row['id'] ?>">
4948
<?php echo $row['title'] ?>
@@ -53,7 +52,7 @@ persisted to the database. Writing in flat PHP is quick and dirty:
5352
</ul>
5453
</body>
5554
</html>
56-
55+
5756
<?php
5857
$link = null;
5958
?>
@@ -81,23 +80,20 @@ Isolating the Presentation
8180
~~~~~~~~~~~~~~~~~~~~~~~~~~
8281

8382
The code can immediately gain from separating the application "logic" from
84-
the code that prepares the HTML "presentation":
85-
86-
.. code-block:: html+php
83+
the code that prepares the HTML "presentation"::
8784

8885
// index.php
8986
$link = new PDO("mysql:host=localhost;dbname=blog_db", 'myuser', 'mypassword');
90-
87+
9188
$result = $link->query('SELECT id, title FROM post');
92-
$result->setFetchMode(PDO::FETCH_ASSOC);
93-
89+
9490
$posts = array();
95-
while ($row = $result->fetch()) {
91+
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
9692
$posts[] = $row;
9793
}
98-
94+
9995
$link = null;
100-
96+
10197
// include the HTML presentation code
10298
require 'templates/list.php';
10399

@@ -142,35 +138,33 @@ Isolating the Application (Domain) Logic
142138
So far the application contains only one page. But what if a second page
143139
needed to use the same database connection, or even the same array of blog
144140
posts? Refactor the code so that the core behavior and data-access functions
145-
of the application are isolated in a new file called ``model.php``:
146-
147-
.. code-block:: html+php
141+
of the application are isolated in a new file called ``model.php``::
148142

149143
// model.php
150144
function open_database_connection()
151145
{
152-
$link = new PDO("mysql:host=localhost;dbname=blog_db", 'myuser', 'mypassword');
146+
$link = new PDO("mysql:host=localhost;dbname=blog_db", 'myuser', 'mypassword');
147+
153148
return $link;
154149
}
155-
150+
156151
function close_database_connection($link)
157152
{
158153
$link = null;
159154
}
160-
155+
161156
function get_all_posts()
162157
{
163158
$link = open_database_connection();
164-
165-
$result = $link->query('SELECT id, title FROM post');
166-
$result->setFetchMode(PDO::FETCH_ASSOC);
167-
159+
160+
$result = $link->query('SELECT id, title FROM post');
161+
168162
$posts = array();
169-
while ($row = $result->fetch()) {
163+
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
170164
$posts[] = $row;
171165
}
172166
close_database_connection($link);
173-
167+
174168
return $posts;
175169
}
176170

@@ -183,9 +177,7 @@ of the application are isolated in a new file called ``model.php``:
183177
in this example, only a portion (or none) of the model is actually concerned
184178
with accessing a database.
185179

186-
The controller (``index.php``) is now very simple:
187-
188-
.. code-block:: html+php
180+
The controller (``index.php``) is now very simple::
189181

190182
require_once 'model.php';
191183

@@ -263,18 +255,16 @@ an individual blog result based on a given id::
263255
{
264256
$link = open_database_connection();
265257
$id = intval($id);
266-
$result = $link->query('SELECT created_at, title, body FROM post WHERE id = '.$id);
267-
$row = $result->fetch(PDO::FETCH_ASSOC);
258+
$result = $link->query('SELECT created_at, title, body FROM post WHERE id = '.$id);
259+
$row = $result->fetch(PDO::FETCH_ASSOC);
268260

269261
close_database_connection($link);
270262

271263
return $row;
272264
}
273265

274266
Next, create a new file called ``show.php`` - the controller for this new
275-
page:
276-
277-
.. code-block:: html+php
267+
page::
278268

279269
require_once 'model.php';
280270

@@ -352,9 +342,7 @@ You're about to take a **big** step with the application. With one file handling
352342
all requests, you can centralize things such as security handling, configuration
353343
loading, and routing. In this application, ``index.php`` must now be smart
354344
enough to render the blog post list page *or* the blog post show page based
355-
on the requested URI:
356-
357-
.. code-block:: html+php
345+
on the requested URI::
358346

359347
// index.php
360348

@@ -364,19 +352,17 @@ on the requested URI:
364352

365353
// route the request internally
366354
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
367-
if ('/index.php' == $uri) {
355+
if ('/index.php' === $uri) {
368356
list_action();
369-
} elseif ('/index.php/show' == $uri && isset($_GET['id'])) {
357+
} elseif ('/index.php/show' === $uri && isset($_GET['id'])) {
370358
show_action($_GET['id']);
371359
} else {
372360
header('Status: 404 Not Found');
373361
echo '<html><body><h1>Page Not Found</h1></body></html>';
374362
}
375363

376364
For organization, both controllers (formerly ``index.php`` and ``show.php``)
377-
are now PHP functions and each has been moved into a separate file, ``controllers.php``:
378-
379-
.. code-block:: php
365+
are now PHP functions and each has been moved into a separate file, ``controllers.php``::
380366

381367
function list_action()
382368
{
@@ -454,9 +440,7 @@ to interpret each request and return a response. To this end, Symfony provides
454440
both a :class:`Symfony\\Component\\HttpFoundation\\Request` and a
455441
:class:`Symfony\\Component\\HttpFoundation\\Response` class. These classes are
456442
object-oriented representations of the raw HTTP request being processed and
457-
the HTTP response being returned. Use them to improve the blog:
458-
459-
.. code-block:: html+php
443+
the HTTP response being returned. Use them to improve the blog::
460444

461445
// index.php
462446
require_once 'vendor/autoload.php';
@@ -467,9 +451,9 @@ the HTTP response being returned. Use them to improve the blog:
467451
$request = Request::createFromGlobals();
468452

469453
$uri = $request->getPathInfo();
470-
if ('/' == $uri) {
454+
if ('/' === $uri) {
471455
$response = list_action();
472-
} elseif ('/show' == $uri && $request->query->has('id')) {
456+
} elseif ('/show' === $uri && $request->query->has('id')) {
473457
$response = show_action($request->query->get('id'));
474458
} else {
475459
$html = '<html><body><h1>Page Not Found</h1></body></html>';
@@ -481,9 +465,7 @@ the HTTP response being returned. Use them to improve the blog:
481465

482466
The controllers are now responsible for returning a ``Response`` object.
483467
To make this easier, you can add a new ``render_template()`` function, which,
484-
incidentally, acts quite a bit like the Symfony templating engine:
485-
486-
.. code-block:: php
468+
incidentally, acts quite a bit like the Symfony templating engine::
487469

488470
// controllers.php
489471
use Symfony\Component\HttpFoundation\Response;

0 commit comments

Comments
 (0)
0