R
R
Rodion Yurchenko2015-09-23 09:55:00
CodeIgniter
Rodion Yurchenko, 2015-09-23 09:55:00

How to take some functions from controllers to a higher level?

Good afternoon
I comprehend CODEIGNITER
There are some functions that are used in any section of the site ...
T e - these functions need direct access from any controller
How to implement it correctly?
I did this: (but I feel that this is not the right approach)
in routes: further Main.php itself
$route['default_controller'] = 'main';

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once('Base.php');

class Main extends Base {

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

Base class
<?php
class Base extends CI_Controller{

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

And in Base I add all the "universal" functions ....
All other controllers, as well as main, are inherited from Base
How to do it all right?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
lyeskin, 2015-09-23
@lyeskin

Make Helper (if the methods are not related to the controller functionality), otherwise, customize the parent controller, and inherit yours from it.

M
maximw, 2015-09-23
@maximw

If you use these functions in controllers, this is correct.
If you use these functions in templates, then it is logical to make helpers.

K
Kirill Petrov, 2015-09-25
@Recosh

For such lazy cases, I made a model, stuck it into autoload. And then I call anywhere in the template or controller.
Like <?=$this->mymodel->get_url_logo()?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question