Archive for April, 2008

An Introduction to MVC

April 23, 2008

What is MVC?
A car is a good real-world example of MVC. With a car you have two views: the interior and the exterior. Both take input from the controller: the driver. The brakes, steering wheel and other controls represent the model: they take input from the controller (driver) and hand them off to the views (interior/exterior) for presentation.

MVC on the Web
MVC is an Object Oriented (OO) design pattern. A Model-View-Controller uses class to organize Business Logic contained in “Models”, Presentation Logic in “Views” and has an overall flow for the application within a “Controller”.

Figure 1:

The Model
Models are the portion of the MVC application that implements the “Business Logic”. Business logic is any PHP logic relating to how information is stored in your application.

The View
Views are the portion of the MVC application that presents output to the user. The most common output for PHP web applications would be HTML, but views are not restricted to this. Your MVC application might output XML, WML, plain text, images, email or some other content.

The Controller
The controller is the heart of the MVC application. The controller translate the mouse and keyboard inputs from the user, informing the model and/or the view to change as appropriate.

Figure 2:

Benefits and drawback of MVC details here.

TrygveReenskaug quoted about MVC as follows:
• The View is connected to the user’s eyes;
• the Controller is connected to the user’s hands;
• and the Model is connected to the user’s mind.



Smarty template engine

April 2, 2008

Smarty is a template engine for PHP. More specifically, it facilitates a manageable way to separate application logic and content from its presentation.
Benefits of using Smarty template engines:
1. separates code from HTML.
2. When you get used to using template engines, your applications will be developed faster
3. Your source code will be more organized, more clean and easier to understand
4. Teamwork is a lot easier.
Web page designers are not dealing with PHP code syntax, but instead easy-to-use templating syntax not much different than plain HTML. The templates are a very close representation of the final output, dramatically shortening the design cycle.

Code example

Since Smarty separates PHP from HTML, you have two files:

Follow the steps:
1.Download Latest Stable Release Smarty here
2. create two directory in www or htdoces named templates and temp
3. create a html file named test.html in templates dir
4. create a php file named test.php in root dir(i.e,www/htdocs)
step 2:
paste the following codes in test.html file

step 3:
paste the following codes in test.php file

Now browse test.php
output of this script will be…
This is the Smarty basic example.