Extend new CI class

Asked

Viewed 226 times

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 don’t have to "repeat" functions that are used throughout the system... For example: I have a model of clients, and another of employees, both use a function called getById(), I would like to not have to repeat the function and always leave them inside a main controller... To be better organized.

  • getById() is in the model or controller? this method will have some standard implementation?

  • It stays on all models of the system, but as it is a standard, I want to leave it on a controller only, and call when I need it... so with the other functions I have

  • @rray I wish I had a file where I concentrated all the functions that are needed in the system, which are standard, without I need to copy and paste the function in each model. Did you understand?

  • Depending on the case you can create a library for this, or make your model implement a interface crud, which would have the methods, save(), listAll(), find(), delete() etc.

  • Yes, just this idea of mine... have a part, so I call when I need to use.

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 inside the briefcase application/core.

And declares it so:

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

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

Then in your controller you extend BILL_Controller instead of CI_Controller.

class Page_Controller extends BILL_Controller
{
    public function minha_acao()
    {
         $this->getById();
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.