Introduction to MVC using PHP
- Friday, December 26, 2008, 8:23
- Featured, PHP & MySQL
- 14 comments
To better demonstrate how MVC works, let’s use the example of a simple data retrival both using MVC and not using it.
One of my amigo asked me to demonstrate how MVC pattern actually works. We actually know what’s Model,View and Controller are all about.Anyhow let me explain them in detail before proceeding with code area. The Traditional way of connecting to the Database and fetching the results.
<?php
$link = mysql_connect(‘localhost’, ‘mysql_user’, ‘mysql_password’);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
echo ‘Connected successfully’;
mysql_close($link);
$query_results = mysql_query(’select * from searchNames order by firstname desc’);
?>
<html>
<body>
<h1>List of Datas</h1><?php while ($row = mysql_fetch_object($query_results)) { ?>
<h2><?php echo $row->firstname ?></h2>
<h2><?php echo $row->lastname ?></h2>
<?php } ?>
</body>
</html>
The Traditional way connects to the database with a connection string, and then the $result variable holds a mysql_query. Then We have a common way of displaying the results made through the query.Now lets jump into MVC for the above code.
Responsibilities of the model
The model stores data in properties and provides application-specific methods that set and retrieve that data.The model must also provide a way for views to register and unregister themselves, and it must manage a list of registered views. Whenever the model determines that its state has changed meaningfully, it must notify all registered views.
The below code shows the Model.
<?php
function methodName()
{
$link = mysql_connect(‘localhost’, ‘mysql_user’, ‘mysql_password’);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
echo ‘Connected successfully’;
mysql_close($link);
$query_results= mysql_query(’select * from searchNames order by firstname desc’);$data = array();
while ($row = mysql_fetch_objects($query_results)) {
$data[] = $row;
}
return $data;
}
?>
Responsibilities of the Controller
The controller listens for notifications from the view based on user input and translates that input into changes in the model.
Here is what Controller tells us
<?php
$data= methodName();
display_template(‘data.tpl’);
?>
Responsibilities of the view
The view must create the user interface and keep it up-to-date. The view listens for state changes in the model; when the model changes, the view updates the interface to reflect the change.
Now the Climax the view part
<html>
<body>
<h1>List of Datas</h1><?php foreach ($data as $row) { ?>
<h2><?php echo $row->firstname ?></h2>
<h2><?php echo $row->lastname?></h2>
<?php } ?>
</body>
</html>
How actually it works?
When we call the url [ I use codeIgnitor framework for development ]. http://www.domain-name.com/index.php/theData/ [ Controller ], it interacts with view and the model to display the data. The Controller acts as a medium between view and model.
Popularity: 100% [?]
About the Author
14 Comments on “Introduction to MVC using PHP”
Write a Comment
Gravatars are small images that can show your personality. You can get your gravatar for free today!
One of the best article which describes about the MVC pattern. Kudos
Such a good information no need to use any framework.
The reason why people opt for various framework is the featurs and ability to use their libraries and methods which come in handy.
I personally like codeignitor which is light, but in most big projects i go for my own designed pattern rather than using some other.
such a great topic
http://www.zahipedia.com
nice article,..im still confuse coz it doesnt work on my localhost…
What was the error you got while trying with this piece.
It feels like something is missing here… what does display_template(’data.tpl’) do – what framework is this using. Sorry – I’m not getting this.
Aibe… just tell me what error you are getting when u use the display_template. I will explain you further how it works… it will help you also get that.
Perfect example Vinothbabu!
I think is important to use a framework to use MVC in a best way, a good framework is http://framework.zend.com , is open source, freem and developed by Zend, The PHP company, and is a very good tool for medium and big projects.
Hi
I tried but the below error occurs “Fatal error: Call to undefined function display_template() ”
I save the following in index.php
List of Datas
firstname ?>
lastname?>
And this is under sample.php
Please clarify.
I dont know from where to call the method.
Thanks in advance.
Please clarify.
I dont know from where to call the method.
Thanks in advance.
function display_template(tpl_file) {
import ($tpl_file);
}
function display_template($tpl_file) {
require_once($tpl_file);
}
very simple yet very effective and to-point introduction, thanks