Reputation: 1275
I'm writing a function in my controller; this is supposed to take in a form, process it, register the user in DB, send a confirmation email etc etc.
to avoid this function to be too cluttered, I was thinking of calling some sub-functions (eg:
function registration()
{
//process form..
_insertInDb($formdata)
_send_mail($address);
//load confirmation view..
}
function _send_mail($to)
{
//code here
}
function _insertInDb($formdata)
{
//other code here...
}
I'm not sure whether writing all the functions in the controller would be best practice -maybe I should insert all 'supporting' function (eg send_mail and insertInDb in this example) in another file and then import them?
This would probably make the controller much more readable.. what's your view?
Upvotes: 3
Views: 2692
Reputation: 22867
SOLID's 'S' - Single Responsibility Principle
Move functions to their own class(es) that are responisble for mailing, database inserts etc.
Upvotes: 0
Reputation: 3239
I assume you would use apt layers to handle each step in the process of registration.
From Controller:
From Business layer: ( UserService)
I assure that it provides what you have expected. Correct me if I am wrong..
Welcome with regards...
Upvotes: 1
Reputation: 30646
MVC is simply a presentation pattern, all of your "business logic" should reside outside of your "MVC" code. Your app should function exactly the same of all of MVC code went away and was replaced with something else.
I'd make a separate class, and have the controller invoke an instance of that class, which will make the necessary DB Inserts, send email alerts, do logging, etc.
update
Since CodeIgniter appears to be a php based CMS, I'd recommend starting here for indepth detail -- http://php.net/manual/en/language.oop5.php
Code like this should get you moving the right direction:
<?php
class SimpleClass
{
// property declaration
public $var = 'a default value';
// method declaration
public function displayVar() {
echo $this->var;
}
}
?>
Upvotes: 2