Sjoerd Maessen blog

PHP and webdevelopment

Separating your controller from your view

without comments

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.

  2. <html>
  3. <head><title>Website: blocked</title></head>
  4. <body>
  5. <h1>Dear visitor</h1>,
  6.   <? $getR = mysql_fetch_assoc(mysql_query("SELECT Block_name,Block_type,Block_datum FROM blocks WHERE Block_id={$_blockID}"));  ?>
  7. <p>
  8. <?=datum("d F Y",$getR["Block_datum"]);?>:
  9. <?
  10. if ($getR["Block_type"] == "ip") {
  11.         $val = $getR[‘Block_name’];
  12.         echo " page is blocked by ip <b>{$val}</b>";
  13. } else if ($getR["Block_type"] == "player") {
  14.         $player= getPlayer($getR["Block_name"]);
  15.         $val = $player[‘fullName’];
  16.         echo " is blocked for player<b>{$val}</b>";
  17. } else if ($getR["Block_type"] == "team") {
  18.         $team = getTeam($getR["Block_name"]);
  19.         $val = $team[‘name’];
  20.         echo " is blocked for team <b>{$val}</b>.";
  21. }
  22. ?>
  23. </body>
  24. </html>

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

  2. <?php
  3. $mResult = mysql_query("SELECT Block_name,Block_type,Block_datum FROM blocks WHERE Block_id=’".$iblockID."’");
  4. $aResult = mysql_fetch_assoc($mResult);
  6. // Determine the error message
  7. $sReplaceValue = ;
  8. $sErrorMessage = ‘Unknown error’;
  9. switch($aResult["Block_type"]) {
  10.         case ‘ip’:
  11.                 $sReplaceValue = $aResult[‘Block_name’];
  12.                 $sErrorMessage = " page is blocked by ip <b>%s</b>";
  13.                 break;
  14.         case ‘player’:
  15.                 $aPlayer= getPlayer($getR["Block_name"]);
  16.                 $sReplaceValue = $aPlayer[‘name’];
  17.                 $sErrorMessage = " is blocked for player <b>%s</b>";
  18.                 break;
  19.         case ‘team’:
  20.                 $aTeam = getTeam($aResult[‘Block_name’]);
  21.                 $sReplaceValue = $aTeam[‘name’];
  22.                 $sErrorMessage = " is blocked for team <b>%s</b>.";
  23.                 break;
  24. }
  25. $sErrorMessage = sprintf($sErrorMessage, $sReplaceValue);
  26. $sDate = date("d F Y", $aResult["Block_datum"]);
  27. ?>
  28. <html>
  29. <head><title>Website: blocked</title></head>
  30. <body>
  31. <h1>Dear visitor</h1>,
  32. <p><?php echo $sDate;?>:<?php echo $sErrorMessage;?></p>
  33. </body>
  34. </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)

Business logic / controller
Presentation / view

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).

Business logic / controller
Presentation / view

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

  • the code will be easier to maintain.
  • the logic will be easier to read and understand because it is not mixed with presentation code.
  • It is easier to reuse code if the presentation is not mixed. The layout of a application will almost always be different when writing a new application but the logic behind it can be exactly the same.
  • by using different layers in your application each layer can be developed by another team simultaneously.
  • you will be able to completely change the domain layer to retrieve data from another source then the database without touching the other layers in your application.

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.

Written by Sjoerd Maessen

August 9th, 2010 at 12:08 pm

Leave a Reply