I'm trying to call my core controller class in application/core
from my child controller in application/controllers
. I have Home
controller home.php
in application/controllers
that extends Public_Controller
in application/core
. My home.php
is like this:
<?php
class Home extends Public_Controller
{
public $data = array(
'page' => 'home',
'main_view' => 'home'
);
public function index()
{
$this->load->view($this->layout, $this->data);
}
}
in the core folder, I have public_controller.php
<?php
class Public_Controller extends MY_Controller
{
// Layout untuk "Publik"
public $layout = 'layout';
}
From the code above, MY_Controller also located in core. here's the code
<?php
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
// Autoload model.
$model = strtolower(get_class($this));
if (file_exists(APPPATH . 'models/' . $model . '_model.php')) {
$this->load->model($model . '_model', $model);
}
}
}
So, in summary i have controllers home
in application/controllers
that extends public_controller
in application/core
and the public_controller
extends my_controller
that also in application/core
. I already modified config/config.php by adding this code
function __autoload($class)
{
if(strpos($class, 'CI_') !== 0)
{
@include_once( APPPATH . 'core/'. $class . EXT );
}
}
so I can connect base controllers to controllers by reference from this https://philsturgeon.uk/codeigniter/2010/02/08/CodeIgniter-Base-Classes-Keeping-it-DRY/
But it's still not working. Saying that
Fatal error: Class 'Public_Controller' not found in C:\xampp\htdocs\cipsb\application\controllers\home.php on line 2
Is there anything I can do for solving this problem? Thank you
It looks like i didn't pay attention to Codeigniter version i'm using. I use codeigniter 3.1.0 and the reference I use is for older codeigniter version. For codeigniter 3 just use this in config/config.php
function __autoload($class) {
if (substr($class,0,3) !== 'CI_') {
if (file_exists($file = APPPATH . 'core/' . $class . '.php')) {
include $file;
}
}
}