tags:

views:

47

answers:

2

Hi

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?

+2  A: 

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;
    }
}
?> 
Nate Bross
ok, so now I'll just need to figure out how to create and call a new class in codeigniter..any suggestions? :)
Patrick
See update.....
Nate Bross
A: 

SOLID's 'S' - Single Responsibility Principle

Move functions to their own class(es) that are responisble for mailing, database inserts etc.

Kindness,

Dan

Daniel Elliott