Zend Framework and MVC

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Localization oI E-Governance Project (DIT, UNDP) vishanta.ravamafhigmail.

com 1 , P a g e

What is Zend Framework and MVC?


Prepared bv. Er. Jishanta Ravamafhi, Intl IT Expert, UNDP


Zend Framework is a simple, straightIorward, open-source soItware Iramework Ior PHP 5 designed to
eliminate the tedious details oI coding and let you Iocus on the big picture. Its strength is in its highly-
modular MVC design, making your code more reusable and easier to maintain.

The Zend Framework is a PHP library Ior building PHP web application. It provides a set oI components to
enable you to build PHP applications more easily which will be easier to maintain and extend over the
liIetime oI the application.

MVC stands Ior Model View Controller. It is a design pattern. A design pattern is a code structure that
allows Ior common coding Irameworks to be replicated quickly. One can think oI a design pattern as a
skeleton or Iramework on which applications will be built. PHP Zend Framework is a Iramework based on
MVC architecture.
The traditional approach to build a PHP application is as Iollows:

<?php
include "config.php";
mysql_connect($hostname, $username, $password);
mysql_select_db($database);
?>

<?php include "header.php"; ?>

<h1>Home Page</h1>

<?php
$sql = "SELECT * FROM news";
$result = mysql_query($sql);
?>

<table>
<?php
while ($row = mysql_fetch_assoc($result)) {
?>
<tr>
<td><?php echo $row['date_created']; ?></td>
<td><?php echo $row['title']; ?></td>
</tr>
<?php
}
?>
</table>

<?php include "footer.php"; ?>
Localization oI E-Governance Project (DIT, UNDP) vishanta.ravamafhigmail.com 2 , P a g e

The above code vividly includes everything in a single Iile, meaning, database connection, business logic,
application logic and data presentation in a single Iile as depicted in Iigure below:

Fig : The organization of a typical PHP file

Over the liIetime oI an application this type oI application becomes un-maintainable as the client keeps
requesting changes which are hacked into the code-base in various places. One method oI improving the
maintainability oI the application is to separate out the code on the page into three distinct parts (and
usually separate Iiles) and this requires a Iramework in MVC pattern and Zend Framework is one oI the
Irameworks available.
The essence oI the MVC pattern is that it breaks a project down into three tiers. The Iigure below describes
the e-platIorm model mapped to the standard Model-View-Controller (MVC) pattern.

The MVC pattern is 3-tiers architecture with the Iollowing tiers:

1. Model (M) refers to business logic
2. View (V) refers to user interface
3. Controller (C) refers to application logic
Localization oI E-Governance Project (DIT, UNDP) vishanta.ravamafhigmail.com 3 , P a g e



Fig : The MVC pattern of Zend Framework

1. Model

The model tier consists oI the representation oI the actual data. The model part oI the application is the part
that is concerned with the speciIics oI the data to be displayed and is generally concerned about the
'business logic part oI the application and tends to load and save to databases.

Data access routines and some business logic can be deIined in the model.

2. View

The view tier consists oI the logic that actually deIines how the displayed data looks. It doesn't decide what
the displayed data is just how it looks. In an ideal world, this template contains no logic. It simply takes
the inIormation it's given and displays it. Usually, this is the HTML.

Controllers pass data to each view to render in some Iormat.

Controller
(Application Logic)
View
(User InterIace)
Model
(Business Logic)
Localization oI E-Governance Project (DIT, UNDP) vishanta.ravamafhigmail.com 4 , P a g e

3. Controller

The controller is what actually deIines what the data is. The controller, in Iact, is where all oI the logic
resides. In the Zend Framework, this tier controls the actions to be executed. That action calls back to get
the desired data Irom the model (in other words, the database or other persistent store), then Ieeds those
Iields to the view, which simply displays it in the browser.

The controller ties together the speciIics oI the model and the view to ensure that the correct data is
displayed on the page. Controllers bind the whole pattern together. They manipulate models, decide which
view to display based on the user's request and other Iactors, pass along the data that each view will need,
or hand oII control to another controller entirely.

Building to the MVC pattern is much easier than building an application in which everything is mashed
together.

In a nutshell:









Localization oI E-Governance Project (DIT, UNDP) vishanta.ravamafhigmail.com 5 , P a g e

In the MVC Iramework that is created in this tutorial, several key points will be raised. The Iirst is the
Iramework needs a single point oI entry, ie: index.php. This is where all access to the site must be
controlled Irom. To ensure that a single point oI entry is maintained, htaccess can be utilized to ensure no
other Iile may be accessed, and that we hide the index.php Iile in the url. Thus creating SEO and user
Iriendly URL's.

It is beyond the scope oI this tutorial to show how to set up htaccess and modrewrite and more
inIormation on these can be gained Irom the Apache manual. The .htaccess Iile itselI looks like this.

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php

The .htaccess Iile will permit access to the site via urls such as
http://www.example.com/news/show

II you do not have modrewrite available, the entry to the site will be the same, except that the URL will
contain the values needed such as: http.//www.example.com/index.php?rtnews/show
Localization oI E-Governance Project (DIT, UNDP) vishanta.ravamafhigmail.com 6 , P a g e

The Site Structure

In this tutorial several directories are required to hold the various components that make up the MVC
Iramework. The index.php and the .htaccess Iiles will, oI course, reside at the top level. We will need a
directory to hold the application code, and directories Ior the model view and controllers. The site structure
should look like this:

html
application
controller
includes
model
views
.htaccess
index.php



To sum it up, the Zend Framework:
Is based on PHP
Is object-oriented
Uses the MVC paradigm
Has open source contributors
Has contributors who take responsibility Ior the Iact that their code is not the intellectual property oI
someone else
It also aims to make your programming liIe easier, not just in general, by instituting the MVC pattern, but
also Ior speciIic things you tend to do all the time, like access databases or output to a PDF Iile. (OK
you probably don't output to a PDF Iile all the time. But I'll bet you would iI it were easier.)



Localization oI E-Governance Project (DIT, UNDP) vishanta.ravamafhigmail.com 7 , P a g e

Zend Framework components include (brieflv):
Zend_Controller
This module provides the overall control Ior the application. It translates requests into speciIic
actions and makes sure they get executed.
Zend_Db
This is based on PHP Data Objects (PDO) and provides access to databases in a generic way.
Zend_Feed
This makes it easy to consume RSS and Atom Ieeds.
Zend_Filter
This provides string-Iiltering Iunctions, such as isEmail() and getAlpha().
Zend_InputFilter
To Zend_Filter, this is designed to work with arrays such as Iorm inputs.
Zend_HttpClient
This enables you perIorm HTTP requests easily.
Zend_1son
This enables you to easily translate PHP objects into JavaScript Object Notation, and vice-versa.
Zend_Log
This provides general-purpose logging Iunctionality.
Zend_Mail
This enables you to send text and multipart MIME e-mail.
Zend_Mime
This is used by Zend_Mail to help decode MIME messages.
Zend_Pdf
This enables you to create new PDF documents, and load and edit existing PDF documents.
Zend_Search
This enables you to perIorm sophisticated searches on your own text. For example, you can build a
search engine that returns results based on relevancy or other Iactors.
Zend_Service_Amazon, Zend_Service_Flickr, and Zend_Service_Yahoo
These provide easy access to these Web service APIs.
Zend_View
This handles the "view" portion oI the MVC pattern.
Zend_XmlRpc
This enables you to easily create an XML-RPC client. (Server capabilities are planned Ior the
Iuture.)






Localization oI E-Governance Project (DIT, UNDP) vishanta.ravamafhigmail.com 8 , P a g e

To summarize,

Zend Framework, a time-saving, well-organized, open source framework for developing web
applications in PHP 5.
Model-view-controller (MVC) is both a design pattern and an architectural pattern used in soItware
engineering. SuccessIul use oI the pattern isolates business logic from user interface considerations,
resulting in an application where it is easier to modiIy either the visual appearance oI the application or the
underlying business rules without aIIecting the other. In MVC, the model represents the inIormation (the
data) oI the application and the business rules used to manipulate the data; the view corresponds to elements
oI the user interIace such as text, checkbox items, and so Iorth; and the controller manages details
involving the communication to the model oI user actions such as keystrokes and mouse movements.

Who's Using the Framework?
The Zend Iramework is powering some large PHP applications, such as IBM's QEDWiki project, Magento
(an open-source ecommerce engine), and Right Media Ior its Right Media Exchange, to name three. This
should give you an indication oI the Iramework's take-up -- iI you decide to invest time learning the Zend
Framework, it's a skill that will likely hold you in good stead in the Iuture.

You might also like