Separating your controller from your view

What is a controller?
A controller can best be described as the set of instructions and algorithms that are responsible for making your application actually doing something. Often based on user input. You could say your controller is your business logic.

What is a view?
The view is the presentation of your application, the part that our user sees. The view won’t do any processing (like math, checking data,…) it will just show data.

Why don’t you separate your logic and view?
I don’t know how or I don’t use a framework. Those are both often heard excuses when I ask someone why he doesn’t separate the logic and view of his application. Even when you don’t use a framework your can perfectly separate your logic from your view.

Consider the following example that is based on a real-life application. Don’t mind the code just watch what a difference it makes when you separate your logic and presentation.


<html>
<head><title>Website: blocked</title></head>
<body>
<h1>Dear visitor</h1>,
  <? $getR = mysql_fetch_assoc(mysql_query("SELECT Block_name,Block_type,Block_datum FROM blocks WHERE Block_id={$_blockID}"));  ?>
<p>
<?=datum("d F Y",$getR["Block_datum"]);?>:
<?
if ($getR["Block_type"] == "ip") { 
    $val = $getR['Block_name'];
    echo " page is blocked by ip {$val}";
} else if ($getR["Block_type"] == "player") { 
    $player= getPlayer($getR["Block_name"]);
    $val = $player['fullName'];
    echo " is blocked for player{$val}";
} else if ($getR["Block_type"] == "team") { 
    $team = getTeam($getR["Block_name"]);
    $val = $team['name'];
    echo " is blocked for team {$val}.";
}
?>
</body>
</html>

And now lets look at the same piece of code again but then refactored:


<?php
$mResult = mysql_query("SELECT Block_name,Block_type,Block_datum FROM blocks WHERE Block_id='".$iblockID."'");
$aResult = mysql_fetch_assoc($mResult);

// Determine the error message
$sReplaceValue = '';
$sErrorMessage = 'Unknown error';
switch($aResult["Block_type"]) {
    case 'ip':
        $sReplaceValue = $aResult['Block_name'];
        $sErrorMessage = " page is blocked by ip %s";
        break;
    case 'player':
        $aPlayer= getPlayer($getR["Block_name"]);
        $sReplaceValue = $aPlayer['name'];
        $sErrorMessage = " is blocked for player %s";
        break;
    case 'team':
        $aTeam = getTeam($aResult['Block_name']);
        $sReplaceValue = $aTeam['name'];
        $sErrorMessage = " is blocked for team %s.";
        break;
}
$sErrorMessage = sprintf($sErrorMessage, $sReplaceValue);
$sDate = date("d F Y", $aResult["Block_datum"]);
?>
<html>
<head><title>Website: blocked</title></head>
<body>
<h1>Dear visitor</h1>,
<p><?php echo $sDate;?>:<?php echo $sErrorMessage;?></p>
</body>
</html>

The second code is much easier to read and maintain the first one, with just a minimal of effort. Lets take it a step futher.

Application structures, multitier architecture

A three tier application
One of the simplest architectures is the three tier structure. This architecture is commonly used in applications. (also take a look at MVC)

A four layer application
Often a fourth layer is added, the domain layer. By adding a domain layer we have a distinct between our business logic and the data access layer (mostly a database).

Why do we need to separate them?
There are a lot of advantages when you separate your business logic from your presentation logic.

Conclusion
So even if you don’t use a framework it should be no problem to separate logic and presentation. It isn’t about which framework or structure you use, just use someting that brings some kind of organisation in your applications.