CodeIgniter Forums
CI3 CI_Controller extending - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: CI3 CI_Controller extending (/showthread.php?tid=61460)



CI3 CI_Controller extending - pingwin - 04-16-2015

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!


RE: CI3 CI_Controller extending - InsiteFX - 04-16-2015

You can name the controller class anything you want, but when saved it has to be saved as MY_Controller in ./application/core


RE: CI3 CI_Controller extending - alenn - 04-16-2015

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.


RE: CI3 CI_Controller extending - Hobbes - 04-17-2015

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.