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

555 responses to “Separating your controller from your view”

  1. git.craigsau.dev Avatar

    References:

    Bodybuilding health risks

    References:
    http://62.234.194.66:3000/katharinapolin

  2. eram-jobs.com Avatar

    References:

    Winstrol tabs results

    References:
    https://62.234.182.183/keenanbook4656

  3. 47.115.223.229 Avatar

    References:

    Athletes using steroids articles

    References:
    http://41.59.86.39:3000/germanjeffries

  4. ceedmusic.com Avatar

    References:

    Steroids enlarged heart

    References:
    https://ceedmusic.com/ecvmodesto952

  5. git.adambissen.me Avatar

    References:

    Hgh steroids for sale

    References:
    http://www.shkxrd.com:3000/wiltonlane074

  6. git.adambissen.me Avatar

    References:

    Anabolic steroid abuse side effects

    References:
    https://git.zakum.cn/milesaleman471

  7. gitlab.iplusus.com Avatar

    References:

    Steroids are good

    References:
    http://118.89.79.106/abbiehardiman

  8. git.lolpro11.me Avatar

    References:

    Best pills to take to gain muscle

    References:
    https://ztube.com.br/@elsalaroche68?page=about

  9. https://allasguru.com Avatar

    References:

    How long do anabolic steroids stay in your system

    References:
    http://TeArs.pt/@jermainesander?page=about

  10. emphira.app Avatar

    References:

    Testosterone withdrawal side effects

    References:
    http://47.76.55.15:21108/berndshifflett

  11. jobs.assist24-7.com Avatar

    References:

    Steroid alternatives reddit

    References:
    http://115.159.194.75:4000/karimattson683

  12. kition.mhl.tuc.gr Avatar

    References:

    Steroids.com reviews

    References:
    http://yun.pashanhoo.com:9090/judyfinn40262

  13. jicc.kr Avatar

    References:

    Hgh steroid cycle

    References:
    https://raimusic.vn/gregoriobrim82

  14. flirta.online Avatar

    References:

    Steroids legal in bodybuilding

    References:
    https://live.eposbd.net/@bucksandover51?page=about

  15. 47.109.30.152 Avatar

    References:

    Anabolic steroids are primarily used in an attempt to

    References:
    https://freehaitianmovies.com/@shelleytrainor?page=about

  16. dating.vi-lab.eu Avatar

    References:

    Weightlifter steroids

    References:
    https://git.rmarl.in/dylanbolivar5

  17. 111.229.183.203 Avatar

    References:

    Supplements to build muscle fast gnc

    References:
    https://hiwifi.denq.us:8418/nataliamacaula

  18. http://106.55.174.214 Avatar

    References:

    What do steriods do

    References:
    https://www.propose.lk/@lettieparsons

  19. 10xhire.io Avatar

    References:

    Best muscle building supplement at gnc

    References:
    http://43.139.240.37:17000/redamccart3886

  20. provision-sa.co.za Avatar

    References:

    Anabolic steroids are primarily used in an attempt to

    References:
    https://git.secretserver.club/izettapedigo86

  21. 47.98.183.226 Avatar

    References:

    What happens when you take steroids

    References:
    https://healedonly.com/@deidre33584513

  22. http://play.kkk24.kr Avatar

    References:

    Complications from long term steroid use

    References:
    https://backtowork.gr/employer/natural-ways-to-boost-testosterone/

Leave a Reply

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