CodeIgniter controller using composer not working
I'm trying to extend a CodeIgniter controller in my application using composer, but it doesn't work.
This will give me
Fatal error: Class 'CI_Controller' not found in D:\xampp\htdocs\ci-dev\application\core\MY_Controller.php on line 11
I knew that if I add spl_autoload_register to my config.php it would work, but I want to use composer.
everything is set up here.
I am creating MY_Controller in my app / core / MY_Controller.php
class MY_Controller extends CI_Controller
{
public $data = array();
public function __construct()
{
parent::__construct();
}
}
after that i add admin controller in app / libraries / Admin_Controller.php
class Admin_Controller extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
}
and frontend controller in app / libraries / Frontend_Controller.php
class Frontend_Controller extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
}
This is my default controller index call
class Welcome extends Frontend_Controller {
public function index()
{
$this->load->view('welcome_message');
}
}
I have configured my composer like this in config.php
$config['composer_autoload'] = FCPATH.'../vendor/autoload.php';
and a composer.json file like this
"autoload": {
"files" : [
"application/core/MY_Controller.php",
"application/libraries/Admin_Controller.php",
"application/libraries/Frontend_Controller.php"
]
},
source to share
CodeIgniter loads CI_Controller
vendor / autoload.php after your file, and since you list them under the "files" option in your composer.json, they are included immediately, not on the right when you need them.
This is not only what is causing the error, but it surpasses the whole purpose of using an autoloader - if you explicitly specify the includes, you can just just have require_once
them.
What is common in CI is equal require
or even directly declare your base controller classes from MY_Controller.php - then you know that they will be available exactly when you need it.
But if you insist on loading them through Composer, there is also a work-around-list system / core / Controller.php under the startup files.
source to share