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

211 responses to “Separating your controller from your view”

  1. zabornatorilon Avatar

    I think other web site proprietors should take this website as an model, very clean and wonderful user genial style and design, let alone the content. You are an expert in this topic!

  2. binance account creation Avatar

    Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?

  3. startek bv koop online Avatar

    Thanks for helping out, great info .

  4. service robot Avatar

    I’m extremely impressed with your writing skills and also with the layout on your weblog. Is this a paid theme or did you modify it yourself? Anyway keep up the nice quality writing, it’s rare to see a great blog like this one today..

  5. Kode Referal Binance Terbaik Avatar

    Thanks for sharing. I read many of your blog posts, cool, your blog is very good.

  6. studyleo.com Avatar

    Hey, I think your blog might be having browser compatibility issues.

    When I look at your blog site in Opera, it looks fine but when opening in Internet Explorer,
    it has some overlapping. I just wanted to give you a quick heads up!
    Other then that, excellent blog!

  7. 注册Binance Avatar

    Your point of view caught my eye and was very interesting. Thanks. I have a question for you.

  8. programs Avatar

    If some one desires to be updated with most up-to-date technologies
    therefore he must be visit this website and be up to date everyday.

  9. ai nsfw pic Avatar

    I’m not sure the place you are getting your info,
    however good topic. I must spend some time learning much
    more or working out more. Thanks for wonderful info I was
    in search of this info for my mission.

  10. "oppna binance-konto Avatar

    I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article. https://www.binance.com/register?ref=IXBIAFVY

  11. zumospin no deposit bonus code Avatar

    Uitgebreide recensie van Zumospin Casino: licentie, spelaanbod, bonussen en mobiele app voor spelers uit Nederland. Welkomstbonus tot €150 + 200 spins.

Leave a Reply

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