Welcome Guest, Not a member yet? Register   Sign In
Extending CI_Controller is not working
#1

[eluser]MartinF[/eluser]
Hello,

i am using CodeIgniter 2.0.

I have a base controller class ControllerBase (ControllerBase.php) that extends the CI_Controller.
I have put this in the application/libraries folder.

Code:
class ControllerBase extends CI_Controller
{
    var $_layout;

    function __construct($layout = 'shared/_layout')
    {
        parent::__construct();
        $this->_layout = $layout;
    }

    function setLayout($layout)
    {
        $this->_layout = $layout;
    }

    function loadLayoutView($view, $title = "", $data = null, $return = false)
    {      
        $data['title'] = $title;
        $data['view'] = $this->load->view($view, $data, true);

        return $this->load->view($this->_layout, $data, $return);
    }
}

The idea is that i usually want this to be the class that all my Controllers extends, and I can put all shared functions in only one place.

But whenever i try to extend this class (ControllerBase) from one of my Controllers (in application/controllers) i get an error saying "Fatal error: Class 'ControllerBase' not found in......"

Code:
class Home extends ControllerBase
{
    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $this->loadLayoutView('home/index', 'Home');
    }
}

I have registered ControllerBase in the autoload.php
Code:
$autoload['libraries'] = array('ControllerBase');

The file is being loaded - if i try to echo out something at the top of the file and i dont try to extend it.

I have also tried naming the file with the MY_ prefix but it doesnt make a difference, I still get the error message when i try to extend the class.

It works fine if I put the ControllerBase class in the same file as the Home class

So i suspect it has something to do with the autoloading happening to late in the proccess (after the Home class tries to extend the ControllerBase), or maybe the naming of the file?

I assume application/libraries is the correct place to put the file?

Anyone who can figure out what i am doing wrong ?
#2

[eluser]InsiteFX[/eluser]
MY_Controller extends CI_Controller - place MY_Controller in application/core

Extended Controllers go in application/controllers

Home extends MY_Controller

Read - Phil Sturgeon's CodeIgniter Base Classes: Keeping it DRY

InsiteFX
#3

[eluser]MartinF[/eluser]
Thanks for the reply.

Works perfectly.
But not quite happy with the file name (MY_Controller) ... but i will live with it. Smile

Thanks again.
#4

[eluser]WanWizard[/eluser]
You can change the default 'MY_' prefix to anything you want in the config.
#5

[eluser]ray023[/eluser]
[quote author="InsiteFX" date="1298853162"]MY_Controller extends CI_Controller - place MY_Controller in application/core

Extended Controllers go in application/controllers

...[/quote]

Should this be a listed step in the upgrading documentation?

In 2.01 to 2.02, step 3 mentions that security should be moved...but I don't see any other steps instructiong about what to do with extended controllers.
#6

[eluser]InsiteFX[/eluser]
You need to add this!
Code:
/*
| -------------------------------------------------------------------
|  Native Autoload - by Phil Sturgeon.
| -------------------------------------------------------------------
|
| Nothing to do with config/autoload.php, this allows PHP autoload to work
| for base controllers and some third-party libraries.
|
| If using HMVC you do not need this! HMVC will autoload.
|
| Place this code at the bottom of your application/config/config.php file.
*/
function __autoload($class)
{
    if (strpos($class, 'CI_') !== 0)
    {
        @include_once(APPPATH . 'core/' . $class . EXT);
    }
}

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB