Codeigniter 3: how can I avoid repeating this chunk of code in my controllers? I am working on a basic blog application in Codeigniter 3.1.8 and Bootstrap 4.

Several entities are present in all controllers (except Login.php and Register.php): static data, categories and pages.

$data = $this-&gt;Static_model-&gt;get_static_data();
$data['pages'] = $this-&gt;Pages_model-&gt;get_pages();
$data['categories'] = $this-&gt;Categories_model-&gt;get_categories();


Further more, in most controller, the code above appears more then one time.

I am afraid this is mot the only case of repetitive code in the application. (See the entire application, at its current state, on my Github account).

I am looking for specific and/or general advice from experienced PHP developers that would help me reduce code redundancy and make it more efficient. 

What is the best way to avoid the repeating of the code above in my controllers?
I had a very short look at lightblog/application/controllers/Pages.php and I noticed that these two values were not used in the page() method:

$data['pages']
$data['posts']


Then I realized that you hand over data to other objects and they might be using these two values. Who knows? This makes your code very hard to understand and to debug. 

Basically you're treating the $data array as a sort of object you hand over to other classes.

But wait, did I say 'object'? But what if it was? Clearly you have a need to keep data accessible, but why use an array that you need to recreate every time? Why not use a class?

Now I don't have any idea how your code is structure, and which class would be the best to use for this. But it would make sense to extend the basic CodeIgniter controller class to hold this data, like so:

class Data_Controller extends CI_Controller 
{
    private $data;

    public function __construct()
    {
        parent::__construct();
        $this-&gt;data = $this-&gt;Static_model-&gt;get_static_data();
        $this-&gt;data['pages'] = $this-&gt;Pages_model-&gt;get_pages();
        $this-&gt;data['categories'] = $this-&gt;Categories_model-&gt;get_categories();
        $this-&gt;data['posts'] = $this-&gt;Posts_model-&gt;sidebar_posts($limit=5, $offset=0);
        $this-&gt;data['page'] = $this-&gt;Pages_model-&gt;get_page($page_id);
    }


    public function getData()
    {
        return $this-&gt;data;
    }

}


or something to the same effect and then extend this for your pages class:

class Pages extends Data_Controller {

    public function __construct()
    {
        // currently this whole method doesn't do anything, it can be removed
        parent::__construct();
    }

    public function page($page_id) {
        $data = $this-&gt;getData();
        ..........................

    }

}


And, of course, those controller classes that don't need all that data can just extend CI_Controller.

I'm sure there are other ways of doing this. I don't like the fact that now you still collect all that information without actually knowning if it is needed. I would make the retrieval of the data dependent on the fact that it is actually used. In other words use getter methods, not a data array. I'll give an example for 'pages' and 'posts', but you have to create the other ones yourself:

class Data_Controller extends CI_Controller 
{

    public function getPages()
    {
        return $this-&gt;Pages_model-&gt;get_pages();
    }

    public function getCatagories()
    {
        return $this-&gt;Categories_model-&gt;get_categories();

    }

}


If you need to buffer the data you can do:

class Data_Controller extends CI_Controller 
{

    public function getCatagories()
    {
        static $buffer = NULL;
        if (is_null($buffer)) $buffer = $this-&gt;Categories_model-&gt;get_categories();
        return $buffer;
    }

}


Keep in mind that this buffer will work for all instances of Data_Controller. That can be an advantage, or a disadvantage.

OK, that was a bit long. I hope you got some new ideas from my ramblings.