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.
cx2fte
🗝 Ticket; TRANSFER 1.899666 BTC. Continue => https://graph.org/Payout-from-Blockchaincom-06-26?hs=e046a6843b46d09bbe389a45dd0eb20d& 🗝
18 Jul 25 at 5:42 am
5j2wex
🛡 + 1.110968 BTC.NEXT - https://graph.org/Payout-from-Blockchaincom-06-26?hs=e046a6843b46d09bbe389a45dd0eb20d& 🛡
19 Jul 25 at 10:37 am
m7mx10
* * * Snag Your Free Gift * * * hs=e046a6843b46d09bbe389a45dd0eb20d* ххх*
3 Aug 25 at 6:58 pm
levitra orodispersible kaufen levitra customer reviews levitra copay assistance
levitra cost
6 Aug 25 at 2:57 am
buy cheap androxal purchase england
order androxal uk pharmacy
androxal drug no prescription
16 Aug 25 at 5:47 pm
canadian enclomiphene online
enclomiphene fedex cod
order enclomiphene generic dosage
16 Aug 25 at 5:48 pm
how can i order rifaximin without a perscrption
canadian pharmacy rifaximin online
get the best rifaximin tablets rifaximin
17 Aug 25 at 2:47 am
get xifaxan uk buy online
purchase xifaxan price netherlands
ordering xifaxan australia online generic
17 Aug 25 at 2:49 am
buying staxyn generic in us
buying staxyn online no rx
buy cheap staxyn cheap online canada
17 Aug 25 at 4:16 am
order avodart purchase tablets
ordering avodart cost at costco
online order avodart generic canadian
17 Aug 25 at 4:16 am
fedex dutasteride without priscription
free dutasteride prices
dutasteride on line cash on delivery
17 Aug 25 at 5:35 am
cheapest buy flexeril cyclobenzaprine online no rx
discount flexeril cyclobenzaprine usa sales
buying flexeril cyclobenzaprine buy germany
17 Aug 25 at 5:35 am
discount gabapentin canada no prescription
how can i order gabapentin without a perscrption
buy gabapentin spain over the counter
17 Aug 25 at 6:34 am
how to buy fildena price new zealand
cheapest buy fildena uk buy cheap
order fildena generic where to buy
17 Aug 25 at 6:37 am
itraconazole cod
how to order itraconazole new zealand buy online
buy itraconazole in Chicago
17 Aug 25 at 7:29 am
přes přepážku kamagra
koupit kamagra přes noc dodání
přes přepážku kamagra alternativa
17 Aug 25 at 7:34 am
prix du kamagra en pharmacie
medicament kamagra medicament medicament
vivo en bradenton fl usa donde puedo comprar kamagra
17 Aug 25 at 8:14 am
hy8m6n
📚 ⚡ Instant Transfer - 0.35 Bitcoin sent. Finalize now => https://graph.org/GET-FREE-BITCOIN-07-23?hs=e046a6843b46d09bbe389a45dd0eb20d& 📚
18 Aug 25 at 3:52 pm
s2a9dq
🖥 Account Notice - 1.9 Bitcoin transfer attempt. Authorize? => https://graph.org/TAKE-YOUR-BITCOIN-07-23?hs=e046a6843b46d09bbe389a45dd0eb20d& 🖥
22 Aug 25 at 1:15 am
mi0o70
🔏 📢 Reminder - 1.6 BTC ready for withdrawal. Proceed > https://graph.org/EARN-BTC-INSTANTLY-07-23?hs=e046a6843b46d09bbe389a45dd0eb20d& 🔏
30 Aug 25 at 6:40 pm