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. 36.153.162.171 Avatar

    References:

    Trenbolone fat loss

    References:
    http://106.55.61.128:3000/joiedanford375

  2. casino kingdom login Avatar

    Honest Kingdom Casino NZ review: 200NZ$ bonus for new players, 100 free spins, VIP program, fast withdrawals. Licensed casino for New Zealand.

  3. www.51kimi.cn Avatar

    References:

    Steroids are a type of

    References:
    https://datingmywish.com/@averymattson68

  4. gitea.nkal.me Avatar

    References:

    Deca durabolin reviews

    References:
    http://47.94.55.54:3000/troy1583938193

  5. casino kingdom Avatar

    Honest Kingdom Casino NZ review: 200NZ$ bonus for new players, 100 free spins, VIP program, fast withdrawals. Licensed casino for New Zealand.

  6. 47.103.48.2 Avatar

    References:

    Safe bodybuilding steroids

    References:
    https://git.vezpi.com/rebekahcallagh

  7. topgtv.com Avatar

    References:

    Ripped muscle x at gnc

    References:
    http://82.156.89.21:3000/cameroncavines

  8. icmimarlikdergisi.com Avatar

    References:

    Non steroid bodybuilders

    References:
    https://git.cyberuk.me/kerricogburn54

  9. 139.196.82.227 Avatar

    References:

    What’s the best testosterone steroid

    References:
    https://gitea.shizuka.icu/doloresmjk8666

  10. yourclipz.com Avatar

    References:

    Over the counter steroids pills

    References:
    https://www.fepp.org.ec/celestahollway

  11. https://clipbeans.com/ Avatar

    References:

    Steroids for bodybuilding for sale

    References:
    https://gitea.biboer.cn/lillianyce2007

  12. 47.99.119.173 Avatar

    References:

    Gnc weight loss mens

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

  13. www.bongmedia.tv Avatar

    References:

    What can steroids do to you

    References:
    https://git.meshel.cn/brentonscobie

  14. 39.101.170.62 Avatar

    References:

    Legal steroids for muscle building

    References:
    https://4realrecords.com/shannoncockeri

  15. git.clickforadventure.co Avatar

    References:

    All of the following are common side-effects of ingesting anabolic steroids except:

    References:
    https://freshteenclips.com/@cjtbetty295721?page=about

  16. 1.95.120.11 Avatar

    References:

    Bodybuilding without steroids

    References:
    https://volts.howto.co.ug/@redashipley019

  17. www.heyne.cloud Avatar

    References:

    Why are steroids prescribed

    References:
    http://218.245.96.10/laurij27614435

  18. https://lawrencewilbert.com Avatar

    References:

    Classification of steriods

    References:
    https://git.sophiagwen.au/helen26v334655

  19. syq.im Avatar

    References:

    Strongest legal steroids

    References:
    https://vidspaceaiapp.com/@allieheller57?page=about

  20. gbewaaplay.com Avatar

    References:

    Illegal supplements

    References:
    http://101.37.69.204:3000/margokunkel80

  21. http://z.duowenlvshi.com/ Avatar

    References:

    Signs a woman is using steroids

    References:
    https://git.vsadygv.com/staciacoury194

  22. https://gividsocial.com Avatar

    References:

    Best steroids for weight loss

    References:
    https://aladin.tube/@cassandranorth?page=about

  23. www.fepp.org.ec Avatar

    References:

    Does the rock use steriods

    References:
    http://115.190.214.62:3001/rufuskeeney696

  24. http://120.201.125.140/ Avatar

    References:

    Steroid injection for muscle growth

    References:
    https://mambotango.it/candymacdougal

  25. 118.195.135.194 Avatar

    References:

    Best bulking cycle stack

    References:
    http://47.98.183.226:3000/rudolffreeman1

  26. 111.230.9.98 Avatar

    References:

    Steroids to get ripped

    References:
    https://mailppto.top/jeanamagarey79

  27. hearty.rip Avatar

    References:

    Nandrolone bodybuilding

    References:
    http://39.99.175.172:8000/olhsal6506120

  28. https://git.suo0.com Avatar

    References:

    2ahukewik8-seu8vnahuhgz4khrreaaqq_auoaxoecaeqaq|the best steroids for muscle growth

    References:
    http://106.55.174.214:3000/virgieclevenge

  29. codes.tools.asitavsen.com Avatar

    References:

    Best steroids for lean muscle mass

    References:
    http://47.98.183.226:3000/joeystreet0552

  30. jinhon-info.com.tw Avatar

    References:

    How safe are steroids

    References:
    http://106.52.71.204:9005/ernestferretti

Leave a Reply

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