Welcome Guest, Not a member yet? Register   Sign In
Fatal error when extending a Controller, not found
#1

[eluser]Isern Palaus[/eluser]
Hello,

I'm trying to extend a controller and I get a not found. My idea is to have BackendController and FrontendController that extends MY_Controller, all in /libraries. In my controllers I will extend Front or Backend depending of the kind of controller.

/libraries/MY_Controller.php
Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Controller extends Controller
{
    function MY_Controller()
    {
        parent::Controller();
        
        $general['base_url'] = $this->config->item("base_url");
        
        $this->load->vars('general', $general);
    }
    
    protected function is_ajax() {
        return ($this->input->server('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest') ? TRUE : FALSE;
    }
}

/libraries/BackendController.php
Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class BackendController extends MY_Controller
{
    function BackendController()
    {
        parent::MY_Controller();
        
        $this->output->enable_profiler(FALSE);
    }
}

/controllers/admin/test.php
Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Test extends BackendController
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function index()
    {
        echo "Works!";
    }
}

And my final result: Fatal error: Class 'BackendController' not found in /var/www/testapplication/src/application/controllers/admin/test.php on line 4

How I can solve it? If I use MY_Controller works fine...

Thanks in advance!
#2

[eluser]Phil Sturgeon[/eluser]
You've missed out the autoload. A class wont exist if you dont include it somehow.

http://philsturgeon.co.uk/news/2010/02/C...ing-it-DRY
#3

[eluser]Isern Palaus[/eluser]
Was some months without CI and didn't remembered why the other times worked. Now I remember! Thank you! :-)
#4

[eluser]Bramme[/eluser]
Gonna dig this up a little, just wanna check.

I have extended CI_Controller by adding MY_Controller.php in application/libraries/
Code:
class MY_Controller extends CI_Controller {

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

}


I extend my controllers from MY_Controller instead of CI_Controller, however, without adding following code (from the link above), I get a "Class 'MY_Controller' not found". So this is "normal" behavior? I'm using CI2.0, shouldn't that autoload function be added to the core version then?




Theme © iAndrew 2016 - Forum software by © MyBB