PHPBuilder - Compose a MVC Paradigm for PHP with Symfony



RSS Twitter
Articles Application Architecture

Compose a MVC Paradigm for PHP with Symfony

by: Octavia Andreea Anghel
|
January 21, 2010

As you probably know, Model-View-Controller (MVC) is a programming technique that Wikipedia defines as: "an architectural pattern used in software engineering. The pattern isolates 'domain logic' (the application logic for the user) from input and presentation (GUI), permitting independent development, testing and maintenance of each." If you apply this definition to PHP, you may very well come up with the Symfony framework. Symfony is an open-source PHP web framework that provides architecture, components and tools for developers to build and maintain complex web applications faster. The Symfony framework uses the MVC architecture to make the web applications a lot easier and faster to modify.
Symfony generates the front controller MVC logic component. The front controller and the layout are common to all actions in a Symfony-based application. You can use Symfony's Propel library to generate the classes of the model layer automatically. By providing a simple API for storing and retrieving data, the Propel library allows you to access your database using a set of objects. The database implementation is invisible to you. If you want to change the database, all you need do is modify a parameter. The view logic is a simple configuration file, which you can easily modify as you like.
Figure 1 shows the three components necessary for a single web page: the model layer, view layer, and controller layer.



Click here for larger image

Figure 1. The MVC Architecture

Create a Demo Application with Symfony

The application in this article is a sample Symfony project that demonstrates how you can use Symfony to develop professional applications with style and little effort. The demo project will be named bookshop, and it will represent an online bookstore. Because describing the entire application is beyond the scope of this article, it explains how to develop only a small part of it. But it includes enough to show what Symfony can do. To be more exact, the portion of the application demonstrated here is the mechanism for seeing the books, the function for deleting/editing/adding a book, and the view for searching for a book. As you will see, Symfony will do most of the work. Let's get started.

Install Latest Symfony Release

To install the Symfony 1.4 framework, you need a web server (Apache, for instance), a database engine (MySQL, PostgreSQL, SQLite, or any PDO-compatible database engine), and PHP 5.2.4 or later. Take the following steps to install Symfony 1.4:
  1. Download the latest stable version archive (1.4) to your computer.
  2. Unzip the archive in your favorite location (this example uses the {C:\demo\} folder), and rename it symfony.
  3. Navigate to the Symfony installation location, create the lib\vendor project directory, and move the unzipped archive symfony there.

Generating an Empty Symfony Project

To create a new project in Symfony, use the 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:project task has created a symfony shortcut 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=Propel option in the creation command.

Generate the Frontend Application

To generate the frontend application, you need to run the generate:app task like this:
on Windows
c:\demo> php symfony generate:app frontend

on Linux
$ php symfony generate:app frontend
Based on the application name passed as an argument, the 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

Configure the Apache Server

The empty application is almost ready to run, but you need to change your Apache configuration first to make the new project accessible. For this, you must edit the 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>
Now save the modifications, restart Apache, and access the http://localhost/index.php page. You should see something like Figure 2, a temporary page that states that your first Symfony project was created.



Click here for larger image

Figure 2. The First Created Project Using Symfony

Go further and access the application in the development environment by typing in the following URL:
http://localhost/frontend_dev.php/
The web debug toolbar should show on the top right corner, including small icons proving that your 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

The web debug toolbar is present on all pages in the development environment and you can click on its links to get more information in this stage. For example, by clicking the logs link you will obtain the logs for the current request (see Figure 4).



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.

1
|
2
Next Page »

Download: SymfanyBookshopDemo.zip

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Octavia Andreea Anghel

Comment:



Comment:

(Maximum characters: 1200). You have characters left.