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. music.michaelmknight.com Avatar

    References:

    Where can i buy anadrol

    References:
    https://jomowa.com/@phoebevzq87373

  2. vidspaceaiapp.com Avatar

    References:

    Most dangerous steroid

    References:
    https://gitea.nkal.me/kathie1648927

  3. katambe.com Avatar

    References:

    Where to get steroids in the us

    References:
    https://git.mana-web.com/deborapalmquis

  4. http://211.149.155.157/ Avatar

    References:

    Why steroids should be legal in baseball

    References:
    https://git.warze.org/georgianna29k

  5. xianyuhome.cn Avatar

    References:

    Anabolic steroid side effects

    References:
    http://z.duowenlvshi.com/troy82m8542382

  6. smusic.sochey.com Avatar

    References:

    Steroid starter pack

    References:
    https://nauticauruguay.com/leahmarrone242

  7. prescription antibiotics

    prescription antibiotics

  8. 49.232.143.93 Avatar

    References:

    What do steroids contain

    References:
    http://220.205.16.27:18081/susannahkierna

  9. git.fast-blast.uk Avatar

    References:

    Cutting pills bodybuilding

    References:
    http://41.59.86.39:3000/germanjeffries

  10. gitea.smatec.com.vn Avatar

    References:

    Steroid alternatives 2015

    References:
    https://gitea.jsjymgroup.com/otiliamack5703

  11. rapid.tube Avatar

    References:

    Deca steroids reviews

    References:
    https://chinami.com/@bryonrabin858?page=about

  12. 47.92.23.195 Avatar

    References:

    What do steroids do to women’s bodies

    References:
    https://mginger.org/@jacquelinemacg

  13. https://git.fs.cs.uni-frankfurt.de Avatar

    References:

    What are the advantages and disadvantages of common names

    References:
    http://209.127.59.74:3000/bryanpaxton398

  14. gitea.belanjaparts.com Avatar

    References:

    Steroid high cholesterol

    References:
    https://git.hilmerarts.de/shantellarmste

  15. git.ewengadonnaud.xyz Avatar

    References:

    Steroids effects on women

    References:
    https://hellomusic.app/traceyopitz932

  16. 115.159.107.117 Avatar

    References:

    What do steroids do to you

    References:
    http://150.158.37.69:3000/colettemacredi

  17. https://git.hilmerarts.de/cliff400687871 Avatar

    References:

    Best legal muscle building supplement

    References:
    https://mginger.org/@jacquelinemacg

  18. http://41.59.86.39:3000/germanjeffries Avatar

    References:

    Bodybuilding steroids side effects

    References:
    https://laviesound.com/reinaldocazaly

  19. gitea.zczcfiles.com.cn Avatar

    References:

    How does anabolic steroids affect the body

    References:
    http://119.3.186.224:3000/rodrickoshea9

  20. 118.178.172.49 Avatar

    References:

    How to cycle steroids

    References:
    https://gitea.nacsity.cn/sharronwahl494

  21. git.dglyoo.com Avatar

    References:

    Steroids health risks

    References:
    https://mp3diary.com/brandengarvan6

  22. ceedmusic.com Avatar

    References:

    Top 10 legal steroids

    References:
    https://tcodpractice.com/randalmusgrave

  23. 39.171.252.63 Avatar

    References:

    Can you take clenbuterol with testosterone

    References:
    https://simapodcast.co.ls/@faeflood828686?page=about

  24. www.wealthtv.tz Avatar

    References:

    Best legal supplements for cutting

    References:
    https://git.micahmoore.io/kathrinp062131

  25. git.adityagupta.dev Avatar

    References:

    Can you buy steroids legally

    References:
    https://xonnon.com/@julianeborden?page=about

  26. www.nemusic.rocks Avatar

    References:

    Weight loss steroids clenbuterol

    References:
    http://81.70.102.154/floydgaylord7

  27. 47.100.44.145 Avatar

    References:

    Best stack for muscle gain

    References:
    https://m.madeu.co.kr/kerryschilling

  28. app.venusroyale.date Avatar

    References:

    What does steroids do to women

    References:
    http://111.228.56.162:3000/corydemaio0448

  29. lafffrica.com Avatar

    References:

    Dianabol for beginners

    References:
    http://150.158.37.69:3000/franciscalampe

  30. bfreetv.com Avatar

    References:

    Brain enhancing drugs for sale

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

  31. git.123doit.com Avatar

    References:

    Legal muscle building supplements

    References:
    http://git.yanei-iot.com:600/pollykeen8278

Leave a Reply

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