Use of Model View Controller
Model View Controller also known as MVC is a great way to do development. But what actually is MVC? It is not a tool or software that you can actually buy... well yes you can buy it and you can name it a tool but I will not speak of those made by others but for those hand made by you. OK so what was MVC? MVC is a self made FRAMEWORK. It's idea is to have TREE TIER SOFTWARE DEVELOPMENT.
Those tree layers are Model, View and Controller. In this article I will write mostly about the CONTROLLER and leave you space to think about the other elements while I will try to give you some basic idea about them too.
step 0. We start building some project and we don't know where to start from. Obviously we should start with some configuration file right? That is good... but don't forget the initial planning it is way more important but we skip this now :) After building some config file we need an index.php file which we will enchant with the power of MVC methodology.
So a basic step 0 is to make a config file and index.php with some basic design and idea.
step 1. The next thing we will do is to build our CONTROLLER. It is responsible for controlling the overall process of our application. So it needs to comply with the following things:
- handle URL requests
- process requested information to and from the layers
- handle loading of classes
This is our very basic form.
/we skip some other code that should be there keep that in mind/
Here it is in PHP code:
##### ..:CONFIG FILE:.. #####
require_once("../config.php");
##### ***CONFIG FILE*** #####
##### ..:AUTOLOAD:.. #####
function __autoload($class_name) {
$dirs = array('/experts/','/experts/db/','/experts/user/', '/experts/question/');
array_push($dirs,'/experts/exceptions/');
foreach ($dirs as $dir) {
$classfile = CLASSES . $dir . $class_name . '.php';
if ( file_exists($classfile ) ) {
require_once ($classfile);
}
}
}
##### ***AUTOLOAD*** #####
##### ..:CONTROLLER:.. #####
if (isset ($_REQUEST['module'])) {
require_once(MODULES . basename($_REQUEST['module']) . ".php");
} else {
require_once(MODULES . 'default.php');
}
##### ***CONTROLLER*** #####
So what you see in this code is that we include in the first place the CONFIG file and this shall be clear why. The second thing is making use of AUTOLOAD function in PHP5 which finds for us the needed class and we don't care about including classes we made into the files (modules) we write.
And then the actual CONTROLLER which just looks a GET parameter and sees if its name build with .php exists in the MODULES folder and if yes it loads it... if not it loads a default one.
This is the INDEX.PHP file code... it needs to have some design or some VIEW implementation which you can see how to achive in the bottom (well actually you will have to read a bit more off this site but don't bother I might write about this another time)
step 2. Is the Model... we will call these MODELS - MODULES.
These MODULES work in the following way:
They are loaded by the CONTROLLER and they serve a specific functionality like login/logout. In this Module we work with the functionality (the classes) we wrote. And this is it.
We might do this to work directly with the VIEW as the following code:
// Proccess with user input if given
if (isset($_GET['action'])) {
switch ($_GET['action']) {
case 'login':
require_once(STATIC_MODULES."login/handle_login.php");
break;
case 'logout':
require_once(STATIC_MODULES."login/handle_logout.php");
break;
}
}
// Display the needed Smarty tpl
if (isset($_SESSION['auth'])) {
$user = unserialize($_SESSION['user']);
$user->loadStats();
$smarty->assign("button" , '<a href="index.php?action=logout">
<img src="button logout.png" style="border: 0px;" />
a>');
$smarty->assign("username" , $user->getUsername());
$smarty->assign("title" , $user->getStats()->getTitle());
$smarty->assign("points" , $user->getStats()->getCredits());
$smarty->display("STATIC/login/login.tpl");
} else {
$smarty->display("STATIC/login/login_form.tpl");
}
The Module just looks after a PARAM to be sure if it is set then process to other modules if necessary ... then it unserializes an OBJECT of the user from the SESSION loads the user info and process the needed info for the VIEW (which is SMARTY template engine).
step 3. The VIEW is already mentioned it is just a TPL file of SMARTY ... for more info about this Templating Engine look at smarty.php.net or just google it for more information. In few words the VIEW is a TPL file which has in it those {$variableName} tags which by parsing this file they are replaced with values that we assign through the MODULES (MODEL).
So for all of you PHP geeks which were not known to MVC I think this is enough to see the power you get from this framework... if you don't see it I will mention some things:
- You get a FRAMEWORK for every project you build to use
- It is easily to upgrade and use
- You don't need to touch the files of your framework most of the times only the CONFIG file and then just start building the MODULES
- You easily will redesign the site because of the templates you use
- You write without caring about includes paths or other stuff
- and if you write this good (not that this isn't good but this piece of code is not what it is now at the project we have used it... yes this was working code :) now it is more advanced with more lines of code and complexity but this is its main idea) you will develop you projects twice as fast as you usually do and believe me when I say YOU GOT THE POWER OF Objects AND Templates AND Re-Usability OF CODE and this is what most programmers have difficulties to achieve.
But YES this isn't a topic that I can cover in a hour or too just like this and yes I am not good at large scale Articles which cover future and history and so on so just look at what wikipedia has to offer about this topic... I'll ease you with this direct link to MVC at wikipedia.
I hope I have given you the first steps and thoughts about building your own MVC whether in PHP or some other programming language.
Have fun!
And remember not to hate your code but to have fun with it ;)
2 comments:
For any company members ;)
this information is just an easy example of how to achieve a near MVC model but not the perfect or even not a one that is production ready so please feel free to use my idea to be an basic/foundation of what you planed to do ;)
Greatings Ivo!
Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!
Post a Comment