• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Should this function be in a helper, a controller, a model, or what?

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?

Use MY_Controller if you use it everywhere

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:

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

class MY_Controller extends Controller{

    function MY_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:

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?)

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:

function __construct(){

That seems to work Smile

The MY_Controller has to go into application/core not controllers!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.