Symfony Vs Simple PHP
Simple Blog in flat PHP
you'll build the token blog application using only flat PHP. To begin, create a single page that displays blog entries that have been persisted to the database. Writing in flat PHP is quick and dirty:
// index.php
$link = mysql_connect('localhost', 'myuser', 'mypassword');
mysql_select_db('blog_db', $link);
$result = mysql_query('SELECT id, title FROM post', $link);
?>
<!DOCTYPE html>
<html>
<head>
<title>List of Posts</title>
</head>
<body>
<h1>List of Posts</h1>
<ul>
<?php while ($row = mysql_fetch_assoc($result)): ?>
<li>
<a href="/show.php?id=<?php echo $row['id'] ?>">
<?php echo $row['title'] ?>
</a>
</li>
<?php endwhile; ?>
</ul>
</body>
</html>
<?php
mysql_close($link);
?>
DrawBacks
Note :Symfony2 fully integrates Doctrine1, a library dedicated to database abstraction and mapping.
Isolating the Presentation
The code can immediately gain from separating the application "logic" from the code that prepares the HTML "presentation":
<?php
// index.php
$link = mysql_connect('localhost', 'myuser', 'mypassword');
mysql_select_db('blog_db', $link);
$result = mysql_query('SELECT id, title FROM post', $link);
$posts = array();
while ($row = mysql_fetch_assoc($result)) {
$posts[] = $row;
}
mysql_close($link);
// include the HTML presentation code
require 'templates/list.php';
The HTML code is now stored in a separate file (templates/list.php),
which is primarily an HTML file that uses a template-like PHP syntax:
<!DOCTYPE html> <html> <head> <title>List of Posts</title> </head> <body> <h1>List of Posts</h1> <ul> <?php foreach ($posts as $post): ?> <li> <a href="/read?id=<?php echo $post['id'] ?<"> <?php echo $post['title'] ?> </a> </li> <?php endforeach; ?> </ul> </body> </html>the file that contains all of the application logic - index.php - is known as a
"controller"
. Controller refers simply to the area of your code that processes
user input and prepares the response. In this case, the controller
prepares data from the database and then includes a template to
present that data.
Isolating the Application (Domain) Logic
<?php
// model.php
function open_database_connection()
{
$link = mysql_connect('localhost', 'myuser', 'mypassword');
mysql_select_db('blog_db', $link);
return $link;
}
function close_database_connection($link)
{
mysql_close($link);
}
function get_all_posts()
{
$link = open_database_connection();
$result = mysql_query('SELECT id, title FROM post', $link);
$posts = array();
while ($row = mysql_fetch_assoc($result)) {
$posts[] = $row;
}
close_database_connection($link);
return $posts;
}
Note :The filename model.php is used because the logic and
data access of an application is traditionally known as the "model"
layer. In a well-organized application, the majority of the code
representing your "business logic" should live in the model.controller (index.php) is now very simple :
<?php require_once 'model.php'; $posts = get_all_posts(); require 'templates/list.php';
Isolating the Layout
At this point, the application has been refactored into three distinct pieces offering various advantages and the opportunity to reuse almost everything on different pages. The only part of the code that can't be reused is the page layout. Fix that by creating a new layout.php file:<!-- templates/layout.php --> <!DOCTYPE html> <html> <head> <title><?php echo $title ?></title> </head> <body> <?php echo $content ?> </body> </html>
The template (templates/list.php) can now be simplified to "extend" the layout:
<?php $title = 'List of Posts' ?> <?php ob_start() ?> <h1>List of Posts</h1> <ul> <?php foreach ($posts as $post): ?> <li> <a href="/read?id=<?php echo $post['id'] ?>"> <?php echo $post['title'] ?> </a> </li> <?php endforeach; ?> </ul> <?php $content = ob_get_clean() ?> <?php include 'layout.php' ?>



