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.


Website: blocked

Dear visitor

,

: {$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}."; } ?>

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

%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"]);
?>

Website: blocked

Dear visitor

,

:

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)

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

Database
Domain
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.

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.

Comments

646 responses to “Separating your controller from your view”

  1. dgwork.co.kr Avatar

    References:

    Play online games 3d

    References:
    http://git.dglyoo.com/chloekoertig9

  2. git.csi-kjsce.org Avatar

    References:

    Online gaming websites

    References:
    https://vydiio.com/@logan89159783?page=about

  3. gitea.cfpoccitan.org Avatar

    References:

    Coral casino santa barbara

    References:
    https://git.deadpoo.net/brittbaughman

  4. git.gonethome.id Avatar

    References:

    Winning at blackjack

    References:
    https://git.sunlix.one/redai35207943

  5. https://git.sunlix.one/veronicasinger Avatar

    References:

    Best online casino bonuses in the uk

    References:
    https://git.adambissen.me/mkgkarla955887

  6. dotvdo.com Avatar

    References:

    Ameristar casino east chicago

    References:
    https://we2gotgame.com/videos/@johna74744452?page=about

  7. vila.go.ro Avatar

    References:

    Casinos online bonos

    References:
    https://theudtaullu.com/@santiagochave?page=about

  8. chenxil.top Avatar

    References:

    Ft mcdowell casino

    References:
    https://date-duell.de/@gabriellestein

  9. wopid.io Avatar

    References:

    Online casino games

    References:
    https://askmilton.tv/@kirkfairweathe?page=about

  10. jo-line.eu Avatar

    References:

    Margaritaville casino

    References:
    https://git.123doit.com/keriholton9650

  11. robots.rip Avatar

    References:

    Online slots games

    References:
    http://ydds.cloud:3000/penelopecram6

  12. gitlab.iplusus.com Avatar

    References:

    Hard rock casino hollywood fl

    References:
    https://funnyutube.com/@lashondagragg0?page=about

  13. vila.go.ro Avatar

    References:

    Ho chunk casino

    References:
    https://alurmusic.com/javierlarsen9

  14. https://shirme.com/ Avatar

    References:

    Dos caminos nyc

    References:
    https://fun-md.com/paulakdx224092

  15. cash.com.tr Avatar

    References:

    Red 7 casino

    References:
    https://getskill.work/carmellastillw

  16. git.ibossay.com Avatar

    References:

    Isle casino poker

    References:
    https://friztty.com/@cleo7481204065

  17. like this here continue check here our website informative post my website hop over to this site follow this link visit this page great post to read review find check out this site official source Avatar

    Nice read, I just passed this onto a friend who was doing some research on that. And he actually bought me lunch as I found it for him smile Thus let me rephrase that: Thanks for lunch!

  18. click here i thought about this this link go to this website see this image source go to this web-site follow this link useful content look at these guys Link here link url my latest blog post this link web site Avatar

    Your style is so unique compared to many other people. Thank you for publishing when you have the opportunity,Guess I will just make this bookmarked.

Leave a Reply

Your email address will not be published. Required fields are marked *