CodeIgniter Forums

Full Version: CI3 CI_Controller extending
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi!

I would like to create an Api controller extending the basic CI_Controller.
After that I could extend API_Controller to create separate API controllers like users api etc.

I've just read some solutions for older version but that looks strange for my.
For example adding __autoload function to the config.php.

I tried to put MY_Api.php into application/core folder and it doesn't work.

Thanks for the help!
You can name the controller class anything you want, but when saved it has to be saved as MY_Controller in ./application/core
You can change the prefix of the file name too, that can be done in ./application/config.php.

So you could save PINGWIN_Controller.php in ./application/core.
one thing you can do to achieve this is something like this:

in: application/core
create: MY_Controller.php

in: application/core
create: Api_controller.php

in: MY_Controller.php your code could look like this:

PHP Code:
<?php ┬áif ( ! defined('BASEPATH')) exit('No direct script access allowed');

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

if ( ! class_exists('Api_Controller'))
{
    require_once 
APPPATH.'core/Api_controller.php';
}

/* End of file MY_Controller.php */
/* Location: ./application/core/MY_Controller.php */ 

this way your Api_controller class will be autoloaded with MY_Controller allowing you to extend it in application/controllers just like MY_Controller.