Compose a MVC Paradigm for PHP with Symfony
Tweet

Click here for larger image
Figure 1. The MVC Architecture
- Download the latest stable version archive (1.4) to your computer.
- Unzip the archive in your favorite location (this example uses the {C:\demo\} folder), and rename it symfony.
- Navigate to the Symfony installation location, create the lib\vendor project directory, and move the unzipped archive symfony there.
generate:project task. Open a MS-DOS command prompt (for Windows), navigate through the symfony folder path, and execute this command:
on Windows: c:\demo> php lib\vendor\symfony\data\bin\symfony generate:project --orm=Propel bookshop on Linux: $ php lib\vendor\symfony\data\bin\symfony generate:project --orm=Propel bookshop
This task generates the structure of directories and files in Table 1 under /demo folder. These directories are all necessary for any Symfony project (this is an empty project).
| Table 1. Required Directories for a Symfony Project |
| Directory | Description |
|---|---|
apps/ |
Hosts all project applications |
cache/ |
The files cached by the framework |
config/ |
The project configuration files |
lib/ |
The project libraries and classes |
log/ |
The framework log files |
plugins/ |
The installed plugins |
test/ |
The unit and functional test files |
web/ |
The web root directory |
Note: The
generate:projecttask has created asymfonyshortcut in the bookshop project root directory to simplify the path you have to write when running a task.
Note: Symfony can provide object-relational mapping by using Doctrine ORM or Propel ORM. By default, Symfony uses Doctrine, but you also may use Propel if you create the project by inserting the–orm=Propeloption in the creation command.
generate:app task like this:
on Windows c:\demo> php symfony generate:app frontend on Linux $ php symfony generate:app frontend
generate:app task creates the default directory structure needed for the application under the apps/frontend directory (see Table 2).| Table 2. Default Directory Structure Needed for an Application |
| Directory | Description |
|---|---|
config/ |
The application configuration files |
lib/ |
The application libraries and classes |
modules/ |
The application code (MVC) |
templates/ |
The global template files |
httpd.conf file by adding in the following lines (at the end):
on Windows
# This is the configuration for your project
Listen 127.0.0.1:80
<VirtualHost 127.0.0.1:80>
DocumentRoot "C:\demo\web"
DirectoryIndex index.php
<Directory "C:\demo\web">
AllowOverride All
Allow from All
</Directory>
Alias /sf "C:\demo\lib\vendor\symfony\data\web\sf"
<Directory "C:\demo\lib\vendor\symfony\data\web\sf">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
on Linux
# This is the configuration for your project
Listen 127.0.0.1:80
<VirtualHost 127.0.0.1:80>
DocumentRoot "/home/sfprojects/demo/web"
DirectoryIndex index.php
<Directory "/home/sfprojects/demo/web">
AllowOverride All
Allow from All
</Directory>
Alias /sf "/home/sfprojects/demo/lib/vendor/symfony/data/web/sf"
<Directory "/home/sfprojects/demo/lib/vendor/symfony/data/web/sf">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>

Click here for larger image
Figure 2. The First Created Project Using Symfony
http://localhost/frontend_dev.php/
sf/ alias configuration is correct (see Figure 3).

Click here for larger image
Figure 3. The Page in Figure 2 Including the Web Debug Toolbar

Click here for larger image
Figure 4. The Logs for Current Request
Note: At any given moment, a Symfony project is in one of the following environments:
- Development environment: for programmers to develop the application;
- Test environment: for automatically testing the application;
- Staging environment: for client to test and report bugs;
- Production environment: the
- end user’s environment.
configure:database task, like this:
On Windows: c:\demo>php symfony configure:database "mysql:host=localhost;dbname=symfonydb" root pass On Linux: $ php symfony configure:database "mysql:host=localhost;dbname=symfonydb" root pass
Note: The
configure:databasetask takes three arguments: thePDO DSN, theusername, and thepassword(optional) to access the database.
Note: The Symfony framework supports all PDO-supported databases, like MySQL, PostgreSQL, SQLite, Oracle, MSSQL, and so on.
Note: Theconfigure:databasetask stores the database configuration into the/config/databases.ymlconfiguration file. Instead of using the task, you can edit this file by hand.

config/schema.yml file, like this:
# config/schema.yml
propel:
books:
id: ~
title: { type: varchar(255), required: true }
author: { type: varchar(255), required: true }
publisher: { type: varchar(255), required: true }
price: { type: varchar(255), required: true }
yearofpub: { type: varchar(255), required: false }
schema.yml, you can generate the proper SQL statements by running the propel:build-sql task, like this (the statements will be stored in the data/sql/ directory, optimized for the database engine you have configured, MySQL):
On Windows: c:\demo>php symfony propel:build-sql On Linux: $ php symfony propel:build-sql
data/sql/ folder, you will find the lib.model.schema.sql file with the following content:
# This is a fix for InnoDB in MySQL >= 4.1.x # It "suspends judgement" for fkey relationships until are tables are set. SET FOREIGN_KEY_CHECKS = 0; #----------------------------------------------------------------------------- #-- books #----------------------------------------------------------------------------- DROP TABLE IF EXISTS `books`; CREATE TABLE `books` ( `id` INTEGER NOT NULL AUTO_INCREMENT, `title` VARCHAR(255) NOT NULL, `author` VARCHAR(255) NOT NULL, `publisher` VARCHAR(255) NOT NULL, `price` VARCHAR(255) NOT NULL, `yearofpub` VARCHAR(255), PRIMARY KEY (`id`) )Type=InnoDB; # This restores the fkey checks, after having unset them earlier SET FOREIGN_KEY_CHECKS = 1;
propel:insert-sql task, like this:
On Windows: c:\demo>php symfony propel:insert-sql On Linux: $ php symfony propel:insert-sql
propel:build-model task, like this (the generated PHP files are stored in lib/model/ folder):
On Windows: c:\demo>php symfony propel:build-model On Linux: $ php symfony propel:build-model
data/fixtures/ directory and use the propel:data-load task to load them into the database.fixtures.yml):
# data/fixtures/fixtures.yml books: 1: title: SOA Patterns with BizTalk Server 2009 author: Richard Seroter publisher: Packt price: E25.89 yearofpub: 2009 2: title: Learning FreeNAS author: Gary Sims publisher: Packt price: E17.49 yearofpub: 2008 3: title: MODx Web Development author: Antano Solar John publisher: Packt price: E17.49 yearofpub: 2009 4: title: Apache Maven 2 Effective Implementation author: Brett Porter, Maria Odea Ching publisher: Packt price: E17.49 yearofpub: 2009 5: title: RESTful PHP Web Services author: Samisa Abeysinghe publisher: Packt price: E14.99 yearofpub: 2008
propel:data-load task:
On Windows: c:\demo>php symfony propel:data-load On Linux: $ php symfony propel:data-load
Note: If you have decided to create the tables by writing SQL statements, you can generate the corresponding
schema.ymlconfiguration file by running thepropel:build-schematask:On Windows: c:\demo>php symfony propel:build-schema On Linux: $ php symfony propel:build-schema
Note: You can compress all the tasks executed in this section by running a single one-named
propel:build-all-loadtask, like this:On Windows: c:\demo>php symfony propel:build-all-load On Linux: $ php symfony propel:build-all-load
propel:generate-module task, like this (you generate the book module for the books model):
On Windows: c:\demo>php symfony propel:generate-module --with-show --non-verbose-templates frontend book books On Linux: $ php php symfony propel:generate-module --with-show --non-verbose-templates frontend book books
apps/frontend/modules/book/ directory (explore those files):| Directory | Description |
|---|---|
actions/ |
The module actions |
templates/ |
The module templates |

Click here for larger image
Figure 5. Testing the Book Module in the Browser
layout.php and you can find it in the apps/frontend/templates/ directory. This directory contains all the global templates for an application.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<?php include_http_metas() ?>
<?php include_metas() ?>
<?php include_title() ?>
<link rel="shortcut icon" href="/favicon.ico" />
<?php include_stylesheets() ?>
<?php include_javascripts() ?>
</head>
<body>
<table border="0" width="100%">
<tr>
<td>
<img src="/images/books_banner.png" alt="header"/>
</td>
<form action="" method="get">
<td bgcolor="black">
<font color="white">Search:</font>
<input size="45" type="text" >
<input type="submit" value="Search" >
<font type="arial" size="25" color="yellow">
<a href="/frontend_dev.php/books/new">New book</a></font>
</td>
</form>
</tr>
<tr>
<td align="center" colspan="2">
<?php echo $sf_content ?>
</td>
</tr>
<tr>
<td colspan="2">
<img src="/images/footer.jpg" height="40" width="100%" alt="footer"/>
</td>
</tr>
</table>
</body>
</html>
Note: The stylesheets, images, JavaScript and uploads are stored by default under the
/webfolder (in/css,/images,/js, and/uploadsfolders, respectively). Therefore, you should place your images under the/imagesfolder.

Click here for larger image
Figure 6. Testing the Book Module After Customizing the Web Page
No comments yet.