@@ -30,11 +30,10 @@ persisted to the database. Writing in flat PHP is quick and dirty:
30
30
<?php
31
31
// index.php
32
32
$link = new PDO("mysql:host=localhost;dbname=blog_db", 'myuser', 'mypassword');
33
-
33
+
34
34
$result = $link->query('SELECT id, title FROM post');
35
- $result->setFetchMode(PDO::FETCH_ASSOC);
36
35
?>
37
-
36
+
38
37
<!DOCTYPE html>
39
38
<html>
40
39
<head>
@@ -43,7 +42,7 @@ persisted to the database. Writing in flat PHP is quick and dirty:
43
42
<body>
44
43
<h1>List of Posts</h1>
45
44
<ul>
46
- <?php while ($row = $result->fetch()): ?>
45
+ <?php while ($row = $result->fetch(PDO::FETCH_ASSOC )): ?>
47
46
<li>
48
47
<a href="/show.php?id=<?php echo $row['id'] ?>">
49
48
<?php echo $row['title'] ?>
@@ -53,7 +52,7 @@ persisted to the database. Writing in flat PHP is quick and dirty:
53
52
</ul>
54
53
</body>
55
54
</html>
56
-
55
+
57
56
<?php
58
57
$link = null;
59
58
?>
@@ -81,23 +80,20 @@ Isolating the Presentation
81
80
~~~~~~~~~~~~~~~~~~~~~~~~~~
82
81
83
82
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"::
87
84
88
85
// index.php
89
86
$link = new PDO("mysql:host=localhost;dbname=blog_db", 'myuser', 'mypassword');
90
-
87
+
91
88
$result = $link->query('SELECT id, title FROM post');
92
- $result->setFetchMode(PDO::FETCH_ASSOC);
93
-
89
+
94
90
$posts = array();
95
- while ($row = $result->fetch()) {
91
+ while ($row = $result->fetch(PDO::FETCH_ASSOC )) {
96
92
$posts[] = $row;
97
93
}
98
-
94
+
99
95
$link = null;
100
-
96
+
101
97
// include the HTML presentation code
102
98
require 'templates/list.php';
103
99
@@ -142,35 +138,33 @@ Isolating the Application (Domain) Logic
142
138
So far the application contains only one page. But what if a second page
143
139
needed to use the same database connection, or even the same array of blog
144
140
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 ``::
148
142
149
143
// model.php
150
144
function open_database_connection()
151
145
{
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
+
153
148
return $link;
154
149
}
155
-
150
+
156
151
function close_database_connection($link)
157
152
{
158
153
$link = null;
159
154
}
160
-
155
+
161
156
function get_all_posts()
162
157
{
163
158
$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
+
168
162
$posts = array();
169
- while ($row = $result->fetch()) {
163
+ while ($row = $result->fetch(PDO::FETCH_ASSOC )) {
170
164
$posts[] = $row;
171
165
}
172
166
close_database_connection($link);
173
-
167
+
174
168
return $posts;
175
169
}
176
170
@@ -183,9 +177,7 @@ of the application are isolated in a new file called ``model.php``:
183
177
in this example, only a portion (or none) of the model is actually concerned
184
178
with accessing a database.
185
179
186
- The controller (``index.php ``) is now very simple:
187
-
188
- .. code-block :: html+php
180
+ The controller (``index.php ``) is now very simple::
189
181
190
182
require_once 'model.php';
191
183
@@ -263,18 +255,16 @@ an individual blog result based on a given id::
263
255
{
264
256
$link = open_database_connection();
265
257
$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);
268
260
269
261
close_database_connection($link);
270
262
271
263
return $row;
272
264
}
273
265
274
266
Next, create a new file called ``show.php `` - the controller for this new
275
- page:
276
-
277
- .. code-block :: html+php
267
+ page::
278
268
279
269
require_once 'model.php';
280
270
@@ -352,9 +342,7 @@ You're about to take a **big** step with the application. With one file handling
352
342
all requests, you can centralize things such as security handling, configuration
353
343
loading, and routing. In this application, ``index.php `` must now be smart
354
344
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::
358
346
359
347
// index.php
360
348
@@ -364,19 +352,17 @@ on the requested URI:
364
352
365
353
// route the request internally
366
354
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
367
- if ('/index.php' == $uri) {
355
+ if ('/index.php' === $uri) {
368
356
list_action();
369
- } elseif ('/index.php/show' == $uri && isset($_GET['id'])) {
357
+ } elseif ('/index.php/show' === $uri && isset($_GET['id'])) {
370
358
show_action($_GET['id']);
371
359
} else {
372
360
header('Status: 404 Not Found');
373
361
echo '<html><body><h1>Page Not Found</h1></body></html>';
374
362
}
375
363
376
364
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 ``::
380
366
381
367
function list_action()
382
368
{
@@ -454,9 +440,7 @@ to interpret each request and return a response. To this end, Symfony provides
454
440
both a :class: `Symfony\\ Component\\ HttpFoundation\\ Request ` and a
455
441
:class: `Symfony\\ Component\\ HttpFoundation\\ Response ` class. These classes are
456
442
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::
460
444
461
445
// index.php
462
446
require_once 'vendor/autoload.php';
@@ -467,9 +451,9 @@ the HTTP response being returned. Use them to improve the blog:
467
451
$request = Request::createFromGlobals();
468
452
469
453
$uri = $request->getPathInfo();
470
- if ('/' == $uri) {
454
+ if ('/' === $uri) {
471
455
$response = list_action();
472
- } elseif ('/show' == $uri && $request->query->has('id')) {
456
+ } elseif ('/show' === $uri && $request->query->has('id')) {
473
457
$response = show_action($request->query->get('id'));
474
458
} else {
475
459
$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:
481
465
482
466
The controllers are now responsible for returning a ``Response `` object.
483
467
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::
487
469
488
470
// controllers.php
489
471
use Symfony\Component\HttpFoundation\Response;
0 commit comments