Reputation: 14803
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
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
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
Reputation: 42332
Put all your "utility" functions into a "helper manager" and access that.
http://codeigniter.com/user_guide/general/helpers.html
Upvotes: 4