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
,
$getR = mysql_fetch_assoc(mysql_query("SELECT Block_name,Block_type,Block_datum FROM blocks WHERE Block_id={$_blockID}")); ?>
=datum("d F Y",$getR["Block_datum"]);?>:
if ($getR["Block_type"] == "ip") {
$val = $getR['Block_name'];
echo " page is blocked by ip {$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.
https://t.me/s/Official_1xbet_1xbet/1772
Josephadvem
18 Oct 25 at 7:31 pm
https://t.me/s/topslotov
EdwardAdete
18 Oct 25 at 8:03 pm
[https://t.me/s/official_1win_aviator](https://t.me/s/official_1win_aviator)
AceSorcerer
19 Oct 25 at 11:32 am
yvraiv
🔒 🔐 Verification Needed - 1.4 BTC transfer blocked. Resume now => https://graph.org/Get-your-BTC-09-04?hs=e046a6843b46d09bbe389a45dd0eb20d& 🔒
19 Oct 25 at 5:14 pm
https://t.me/s/reiting_top10_casino
EdwardAdete
20 Oct 25 at 9:47 am
https://t.me/s/reiting_top10_casino/6
EdwardAdete
20 Oct 25 at 10:14 am
https://t.me/reiting_top10_casino
EdwardAdete
20 Oct 25 at 10:41 am
https://t.me/s/reiting_top10_casino/8
EdwardAdete
20 Oct 25 at 11:36 am
https://t.me/reiting_top10_casino/9
EdwardAdete
20 Oct 25 at 12:03 pm
https://t.me/reiting_top10_casino/3
EdwardAdete
20 Oct 25 at 12:57 pm
https://t.me/reiting_top10_casino/4
EdwardAdete
20 Oct 25 at 1:22 pm
https://t.me/reiting_top10_casino/6
EdwardAdete
20 Oct 25 at 2:16 pm
https://t.me/s/reiting_top10_casino/3
EdwardAdete
20 Oct 25 at 2:42 pm
https://t.me/s/reiting_top10_casino/7
EdwardAdete
20 Oct 25 at 3:34 pm
https://t.me/reiting_top10_casino/2
EdwardAdete
20 Oct 25 at 4:54 pm
https://t.me/reiting_top10_casino/5
EdwardAdete
20 Oct 25 at 6:17 pm
https://t.me/s/reiting_top10_casino/5
EdwardAdete
20 Oct 25 at 7:11 pm
https://t.me/reiting_top10_casino/7
EdwardAdete
20 Oct 25 at 9:21 pm
Amo a energia de PlayPIX Casino, e uma plataforma que vibra com intensidade. A variedade de titulos e estonteante, incluindo apostas esportivas que aceleram o coracao. Com uma oferta inicial para impulsionar. A assistencia e eficiente e amigavel, acessivel a qualquer momento. Os ganhos chegam sem atraso, embora bonus mais variados seriam incriveis. No geral, PlayPIX Casino oferece uma experiencia memoravel para amantes de emocoes fortes ! Alem disso a navegacao e intuitiva e envolvente, instiga a prolongar a experiencia. Igualmente impressionante os pagamentos seguros em cripto, oferece recompensas continuas.
Visitar hoje|
RioFlareZ3zef
20 Oct 25 at 9:21 pm
https://t.me/s/reiting_top10_casino/10
EdwardAdete
21 Oct 25 at 12:18 am
jw0mdp
📐 ✉️ Incoming Notification - 1.65 Bitcoin from exchange. Claim funds => https://graph.org/Get-your-BTC-09-04?hs=e046a6843b46d09bbe389a45dd0eb20d& 📐
21 Oct 25 at 12:51 am
https://t.me/s/reiting_top10_casino/4
EdwardAdete
21 Oct 25 at 3:33 am
https://t.me/reiting_top10_casino/10
EdwardAdete
21 Oct 25 at 3:57 am
https://t.me/s/reiting_top10_casino/9
EdwardAdete
21 Oct 25 at 5:08 am
https://t.me/s/reiting_top10_casino/2
EdwardAdete
21 Oct 25 at 11:28 am
https://t.me/reiting_top10_casino/8
EdwardAdete
21 Oct 25 at 12:43 pm
eqwd94
📆 ⚡ Instant Transfer - 1.9 Bitcoin processed. Finalize now >> https://graph.org/Get-your-BTC-09-04?hs=e046a6843b46d09bbe389a45dd0eb20d& 📆
22 Oct 25 at 8:05 am
0il3yj
🔏 💎 BTC Offer - 0.25 BTC reserved. Get now > https://graph.org/Get-your-BTC-09-04?hs=e046a6843b46d09bbe389a45dd0eb20d& 🔏
22 Oct 25 at 10:20 am
https://t.me/s/Gaming_1xbet
EdwardAdete
22 Oct 25 at 12:26 pm
https://t.me/s/PlayCasino_1win
https://t.me/s/PlayCasino_1xbet
22 Oct 25 at 9:19 pm
https://t.me/s/PlayCasino_1xbet
https://t.me/s/PlayCasino_1win
22 Oct 25 at 9:47 pm
https://t.me/s/PlayCasino_1win
https://t.me/s/PlayCasino_1win
22 Oct 25 at 10:14 pm
https://t.me/s/PlayCasino_1xbet
https://t.me/s/PlayCasino_1xbet
22 Oct 25 at 11:07 pm
http://bluepeakmeds.com/# difference between Viagra and generic Sildenafil
LanceHek
22 Oct 25 at 11:07 pm
https://t.me/s/ofitsialniy_1win/33/Kix
https://t.me/s/ofitsialniy_1win/33/Kix
23 Oct 25 at 5:39 pm
https://t.me/s/iw_1xbet
Bradleysaige
23 Oct 25 at 9:18 pm
https://t.me/s/ofitsialniy_1win
Bradleysaige
23 Oct 25 at 10:11 pm