Mala
Mala

Reputation: 14803

CodeIgniter - functions available in multiple controllers

I'm relatively new to CodeIgniter, and so far my project is created entirely using Controllers and Views. However, as it's getting more complex, I'm finding that there are certain functions that I've copy-pasted into multiple controllers -- this is hardly ideal as editing one requires remembering to edit all of the others as well.

There is a plethora of CI features that I know nothing about - models, helpers, extending "Controller", etc. etc. Where should I look to in order to accomplish the above? I suppose I could also import() a block of code directly, although I get the feeling that this is not "the CodeIgniter Way".

Upvotes: 4

Views: 4896

Answers (3)

Arun David
Arun David

Reputation: 2784

base_controller.php

<?php
class Base_Controller extends CI_Controller {
    function __construct()
    {
        parent::__construct();
    }
    function base_function(){

        }
}
?>

other_controller.php

<?php
require_once('base_controller.php');
class Other_Controller extends Base_Controller{
    function __construct()
    {
        parent::__construct();
    }
    function index()
    {
    $this->base_function(); 
    }
}
?>

Upvotes: 2

bl00dshooter
bl00dshooter

Reputation: 991

Or create a base controller, and extend other controllers from it.

I'm sure Phil Sturgeon has a guide on it: http://philsturgeon.co.uk/blog/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY

Upvotes: 2

Dominic Tancredi
Dominic Tancredi

Reputation: 42332

Put all your "utility" functions into a "helper manager" and access that.

http://codeigniter.com/user_guide/general/helpers.html

Upvotes: 4

Related Questions