Welcome Guest, Not a member yet? Register   Sign In
Should this function be in a helper, a controller, a model, or what?
#1

[eluser]w1ntermut3[/eluser]
Several pages on a site I'm building will require 'share this' functionality; the user will be provided with a copy'n'paste shareable URL, or a URL will be posted to their Facebook wall, etc etc ad nauseum. For this, we'd like to generate unique tracking codes to append to the shared URL. Part of generating the tracking code is logging it to the database.

So, I've got to create a function that:
a) Will need to load - or at least, have access to - a DB model
b) Will be called by more than one controller

And I don't know where to put it. Putting identical copies into each controller class seems stupid; from what I understand, putting stuff that needs to load a database model into a helper class is also bad practice.

Where should this function go, in order to best fit the codeigniter MVC structure?
#2

[eluser]osci[/eluser]
Use MY_Controller if you use it everywhere
#3

[eluser]w1ntermut3[/eluser]
Aha. That seems obvious now you say it Smile I'd seen MY_controller mentioned but hadn't considered its use before.

However, following the Wiki guide, I'm having some issues getting it to work. I'm getting an error:

Quote:Fatal error: Class 'MY_Controller' not found in C:\dev\bit_campaign\application\controllers\home.php on line 3

I've created MY_Controller as:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends Controller{

    function MY_Controller ()  {        
        parent::Controller();
    }
    
    private function _generateTrackingCode($share_method, $source_user_id = NULL, $target_user_id = NULL, $video_id = NULL){
        return 'ABC123';
    }

}

/* End of file MY_Controller.php */
/* Location: ./application/libraries/MY_Controller.php */


My Home controller starts:

Code:
class Home extends MY_Controller {

What am I doing wrong?

(Also - the wiki mentions that MY_Controller should extend Controller. However, my understanding was that, from CI 2.0, the core controller class is CI_Controller. Is this correct?)
#4

[eluser]w1ntermut3[/eluser]
Ok, scratch that. The user guide docs seem more up to date.

So MY_Controller should go into /application/core/, it should extent CI_Controller, and the constructor function should look like:

Code:
function __construct(){
    parent::__construct();
}


That seems to work Smile
#5

[eluser]InsiteFX[/eluser]
The MY_Controller has to go into application/core not controllers!

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB