Extend new CI class

3

I have the following code:

class MY_Controller extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

}

I need to build a class where I do the CI_Controller extension, so I do not have to "repeat" functions that are used throughout the system ... For example: I have a client model, and another employee, both use a function called getById (), I would like to not need to repeat the function and always leave them inside a main controller ... To get better organized.

    
asked by anonymous 20.07.2015 / 21:45

1 answer

2

First you do this in your Codeigniter configuration file:

// exemplo com o seu nome no stackoverlow
$config['subclass_prefix'] = 'BILL_';

Then you create the file Bill_Controller.php within the application/core folder.

And it declares it like this:

class BILL_Controller exends CI_Controller
{
    public function __construct()
    {
       parent::__construct();
    }

    protected function getById()
    {
      // Retorne algo aqui
    }
}

Next, in your controller you extend BILL_Controller instead of CI_Controller .

class Page_Controller extends BILL_Controller
{
    public function minha_acao()
    {
         $this->getById();
    }
}
    
17.08.2015 / 16:29