@@ -29,10 +29,9 @@ persisted to the database. Writing in flat PHP is quick and dirty:
29
29
30
30
<?php
31
31
// index.php
32
- $link = mysql_connect('localhost', 'myuser', 'mypassword');
33
- mysql_select_db('blog_db', $link);
32
+ $link = new PDO("mysql:host=localhost;dbname=blog_db", 'myuser', 'mypassword');
34
33
35
- $result = mysql_query ('SELECT id, title FROM post', $link );
34
+ $result = $link->query ('SELECT id, title FROM post');
36
35
?>
37
36
38
37
<!DOCTYPE html>
@@ -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 = mysql_fetch_assoc( $result)): ?>
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'] ?>
@@ -55,7 +54,7 @@ persisted to the database. Writing in flat PHP is quick and dirty:
55
54
</html>
56
55
57
56
<?php
58
- mysql_close( $link) ;
57
+ $link = null ;
59
58
?>
60
59
61
60
That's quick to write, fast to execute, and, as your app grows, impossible
@@ -81,26 +80,24 @@ 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
- $link = mysql_connect('localhost', 'myuser', 'mypassword');
90
- mysql_select_db('blog_db', $link);
86
+ $link = new PDO("mysql:host=localhost;dbname=blog_db", 'myuser', 'mypassword');
91
87
92
- $result = mysql_query ('SELECT id, title FROM post', $link );
88
+ $result = $link->query ('SELECT id, title FROM post');
93
89
94
90
$posts = array();
95
- while ($row = mysql_fetch_assoc( $result)) {
91
+ while ($row = $result->fetch(PDO::FETCH_ASSOC )) {
96
92
$posts[] = $row;
97
93
}
98
94
99
- mysql_close( $link) ;
95
+ $link = null ;
100
96
101
97
// include the HTML presentation code
102
98
require 'templates/list.php';
103
99
100
+
104
101
The HTML code is now stored in a separate file (``templates/list.php ``), which
105
102
is primarily an HTML file that uses a template-like PHP syntax:
106
103
@@ -141,31 +138,29 @@ Isolating the Application (Domain) Logic
141
138
So far the application contains only one page. But what if a second page
142
139
needed to use the same database connection, or even the same array of blog
143
140
posts? Refactor the code so that the core behavior and data-access functions
144
- of the application are isolated in a new file called ``model.php ``:
145
-
146
- .. code-block :: html+php
141
+ of the application are isolated in a new file called ``model.php ``::
147
142
148
143
// model.php
149
144
function open_database_connection()
150
145
{
151
- $link = mysql_connect('localhost', 'myuser', 'mypassword');
152
- mysql_select_db('blog_db', $link);
146
+ $link = new PDO("mysql:host=localhost;dbname=blog_db", 'myuser', 'mypassword');
153
147
154
148
return $link;
155
149
}
156
150
157
151
function close_database_connection($link)
158
152
{
159
- mysql_close( $link) ;
153
+ $link = null ;
160
154
}
161
155
162
156
function get_all_posts()
163
157
{
164
158
$link = open_database_connection();
165
159
166
- $result = mysql_query('SELECT id, title FROM post', $link);
160
+ $result = $link->query('SELECT id, title FROM post');
161
+
167
162
$posts = array();
168
- while ($row = mysql_fetch_assoc( $result)) {
163
+ while ($row = $result->fetch(PDO::FETCH_ASSOC )) {
169
164
$posts[] = $row;
170
165
}
171
166
close_database_connection($link);
@@ -182,9 +177,7 @@ of the application are isolated in a new file called ``model.php``:
182
177
in this example, only a portion (or none) of the model is actually concerned
183
178
with accessing a database.
184
179
185
- The controller (``index.php ``) is now very simple:
186
-
187
- .. code-block :: html+php
180
+ The controller (``index.php ``) is now very simple::
188
181
189
182
require_once 'model.php';
190
183
@@ -261,21 +254,17 @@ an individual blog result based on a given id::
261
254
function get_post_by_id($id)
262
255
{
263
256
$link = open_database_connection();
264
-
265
257
$id = intval($id);
266
- $query = 'SELECT created_at, title, body FROM post WHERE id = '.$id;
267
- $result = mysql_query($query);
268
- $row = mysql_fetch_assoc($result);
258
+ $result = $link->query('SELECT created_at, title, body FROM post WHERE id = '.$id);
259
+ $row = $result->fetch(PDO::FETCH_ASSOC);
269
260
270
261
close_database_connection($link);
271
262
272
263
return $row;
273
264
}
274
265
275
266
Next, create a new file called ``show.php `` - the controller for this new
276
- page:
277
-
278
- .. code-block :: html+php
267
+ page::
279
268
280
269
require_once 'model.php';
281
270
@@ -353,9 +342,7 @@ You're about to take a **big** step with the application. With one file handling
353
342
all requests, you can centralize things such as security handling, configuration
354
343
loading, and routing. In this application, ``index.php `` must now be smart
355
344
enough to render the blog post list page *or * the blog post show page based
356
- on the requested URI:
357
-
358
- .. code-block :: html+php
345
+ on the requested URI::
359
346
360
347
// index.php
361
348
@@ -365,19 +352,17 @@ on the requested URI:
365
352
366
353
// route the request internally
367
354
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
368
- if ('/index.php' == $uri) {
355
+ if ('/index.php' === $uri) {
369
356
list_action();
370
- } elseif ('/index.php/show' == $uri && isset($_GET['id'])) {
357
+ } elseif ('/index.php/show' === $uri && isset($_GET['id'])) {
371
358
show_action($_GET['id']);
372
359
} else {
373
360
header('Status: 404 Not Found');
374
361
echo '<html><body><h1>Page Not Found</h1></body></html>';
375
362
}
376
363
377
364
For organization, both controllers (formerly ``index.php `` and ``show.php ``)
378
- are now PHP functions and each has been moved into a separate file, ``controllers.php ``:
379
-
380
- .. code-block :: php
365
+ are now PHP functions and each has been moved into a separate file, ``controllers.php ``::
381
366
382
367
function list_action()
383
368
{
@@ -455,9 +440,7 @@ to interpret each request and return a response. To this end, Symfony provides
455
440
both a :class: `Symfony\\ Component\\ HttpFoundation\\ Request ` and a
456
441
:class: `Symfony\\ Component\\ HttpFoundation\\ Response ` class. These classes are
457
442
object-oriented representations of the raw HTTP request being processed and
458
- the HTTP response being returned. Use them to improve the blog:
459
-
460
- .. code-block :: html+php
443
+ the HTTP response being returned. Use them to improve the blog::
461
444
462
445
// index.php
463
446
require_once 'vendor/autoload.php';
@@ -468,9 +451,9 @@ the HTTP response being returned. Use them to improve the blog:
468
451
$request = Request::createFromGlobals();
469
452
470
453
$uri = $request->getPathInfo();
471
- if ('/' == $uri) {
454
+ if ('/' === $uri) {
472
455
$response = list_action();
473
- } elseif ('/show' == $uri && $request->query->has('id')) {
456
+ } elseif ('/show' === $uri && $request->query->has('id')) {
474
457
$response = show_action($request->query->get('id'));
475
458
} else {
476
459
$html = '<html><body><h1>Page Not Found</h1></body></html>';
@@ -482,9 +465,7 @@ the HTTP response being returned. Use them to improve the blog:
482
465
483
466
The controllers are now responsible for returning a ``Response `` object.
484
467
To make this easier, you can add a new ``render_template() `` function, which,
485
- incidentally, acts quite a bit like the Symfony templating engine:
486
-
487
- .. code-block :: php
468
+ incidentally, acts quite a bit like the Symfony templating engine::
488
469
489
470
// controllers.php
490
471
use Symfony\Component\HttpFoundation\Response;
0 commit comments