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

377 responses to “Separating your controller from your view”

  1. open binance account Avatar

    Your point of view caught my eye and was very interesting. Thanks. I have a question for you. https://www.binance.com/en-ZA/register?ref=B4EPR6J0

  2. moldova illuminati Avatar

    Hello! I just would like to give a huge thumbs up for the great info you have here on this post. I will be coming back to your blog for more soon.

  3. Illuminati europe Avatar

    Of course, what a great website and enlightening posts, I surely will bookmark your website.Best Regards!

  4. cheap video chat Avatar

    I’ve been surfing online more than three hours today, yet I never
    found any interesting article like yours. It is pretty
    worth enough for me. In my view, if all website owners and
    bloggers made good content as you did, the net will be much more useful than ever
    before.

  5. live cam girls Avatar

    Hi Dear, are you really visiting this site regularly,
    if so afterward you will without doubt get good know-how.

  6. azithromycin for pneumonia

    azithromycin for pneumonia

  7. cialis dapoxetine europe

    cialis dapoxetine europe

  8. doxycycline capsules

    doxycycline capsules

  9. 最佳Binance推荐代码 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?

  10. https://suryapowereng.in/employer/spiropent-tabletten-anwendung/ Avatar

    References:

    2ahukewik8-seu8vnahuhgz4khrreaaqq_auoaxoecaeqaq|the best steroids for muscle growth

    References:
    https://reputable.cc/profile/wilfred3707646

  11. http://55x.top Avatar

    References:

    Winstrol oral

    References:
    https://didadj.com/clarissawight

  12. git.liuhung.com Avatar

    References:

    Paranoid jealousy

    References:
    http://43.138.249.161:3000/alanaweed4463

  13. https://gitea.nkal.me/ Avatar

    References:

    Oral testosterone steroids for sale

    References:
    https://git.binarycat.org/kieranchild809

  14. 175.27.229.211 Avatar

    References:

    How to not gain weight on steroids

    References:
    http://47.103.48.2:3002/vkvlindsay8766

  15. chinami.com Avatar

    References:

    Are there any legal steroids that work

    References:
    https://tubepeer.com/@analisa58n7224?page=about

  16. 119.96.62.56 Avatar

    References:

    Legal body building supplements

    References:
    https://mkhonto.net/@jorgearevalo48?page=about

  17. http://132.232.92.186:3000/alexandriaiyj Avatar

    References:

    What are products that are consumed rapidly and regularly classified
    as?

    References:
    http://119.29.64.167:3000/heikebridgefor

  18. https://volts.howto.co.ug/ Avatar

    References:

    Is testosterone a steroid

    References:
    https://gitea.quiztimes.nl/antoniahinder

  19. diflucan antifungal medication

    diflucan antifungal medication

Leave a Reply

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