Sizzling Code
Sizzling Code

Reputation: 6070

codeigniter, class is not extending from libraries folder

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?

Upvotes: 1

Views: 2963

Answers (3)

Nafiu Lawal
Nafiu Lawal

Reputation: 458

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);
        }
    }
});

Upvotes: 0

Sizzling Code
Sizzling Code

Reputation: 6070

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.

Upvotes: 3

michail1982
michail1982

Reputation: 159

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

Upvotes: 0

Related Questions