1

Here is what i did.

My settings in applications/config/config.php file

$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
$config['url_suffix'] = '';
$config['subclass_prefix'] = 'MY_';

Created file MY_Controller.php in application/core

MY_Controller.php file includes:

class MY_Controller extends CI_Controller
{
    public function __construct(){
        parent::__construct();
    }
} 

created file Frontend_Controller.php in application/libraries

Frontend_Controller.php file includes

class Frontend_Controller extends MY_Controller
{
    public function __construct(){
        parent::__construct();
    }
}

In last I extended the main contrller class here with Frontend_Controller my main controller resides in application/controllers/main.php

class Main extends MY_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('PrizeBondSearch_Model');
    }

    public function index()
    {
        $PrizeBonds = $this->PrizeBondSearch_Model->ShowAllPBS();
        $this->load->view('home', $PrizeBonds);
    }
}

Problem: So here comes the problem, when i extend the main controller class with MY_Controller, it works perfectly fine,

But when i try to extend the main controller with Frontend_Controller class it gives me this below problem

Fatal error: Class 'Frontend_Controller' not found in C:\xampp\htdocs\projects\PrizeBondSearch\application\controllers\main.php on line 3

Any Ideas How to Resolve it?

2
  • 1
    Why you put Controller in libraries folder? Commented Apr 26, 2013 at 14:10
  • this is exactly the question and answer from tutsplus video tutorial course right? Commented Jan 19, 2015 at 12:37

3 Answers 3

3

No worries, Found the Solution at Last.

Needed to load the library classname.

So Added the Below lines in the config.php file.

function __autoload($classname){
    if(strpos($classname, 'CI_')!==0){
        $file = APPPATH.'libraries/'.$classname.'.php';
        if(file_exists($file)&& is_file($file)){
            @include_once($file);
        }
    }
}

It’s working perfectly fine now.

Sign up to request clarification or add additional context in comments.

2 Comments

You should be storing your controllers in application/controllers. The libraries directory is not meant for controllers. You solution is bad practice.
@crypticツ no i what i am trying to have base controllers. not just controllers.
0

include Frontend_Controller.php in begining of MY_Controller.php or use spl_autoloader to prevent this error

Comments

0

Some content of the accepted answer are deprecated in PHP.

Use this function instead in your config.php file

spl_autoload_register(function ($classname) {
    if (strpos($classname, 'CI_') !== 0) 
    {
        $file = APPPATH . 'libraries/' . $classname . '.php';
        if (file_exists($file) && is_file($file)) 
        {
            @include_once($file);
        }
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.