CodeIgniter maintain data through Controllers I have a main menu in my application.  It loads depending on the role of the user that I validate from a table in my data base, but I have to recreate this menu every time I call a new controller.

User Controller:

class User extends Ci_Controller {

    function __construct(){

        parent::__construct();

        if (!$this-&gt;session-&gt;userdata('session_user')) {
            $this-&gt;session-&gt;set_flashdata("mensaje", 'No cuenta con permisos *');
            redirect();
            $this-&gt;session_usuario = 1;
        }

        $session_data = $this-&gt;session-&gt;userdata('session_user');
        $image        = $session_data['img'];
        $nickname     = $session_data['nick'];
        $res          = "";

        $this-&gt;layout-&gt;setLayout('layout/admin');
        $this-&gt;layout-&gt;setImage(base_url().$image);
        $this-&gt;layout-&gt;setNick($nickname);

        $data['module'] = $this-&gt;usuarios_model-&gt;seleccionarModulos($session_data['perfil'] );

        for ($i=0; $i &lt; count($data['module']) ; $i++) {                                                    
            $info['functionality'] = $this-&gt;usuarios_model-&gt;seleccionarFuncionalidades(     $session_data['usuario'], $data['module'][$i]['id']);
            $data['module'][$i]['route'] = $this-&gt;load-&gt;view($data['module'][$i]['route'], $info, true);
        }

        foreach ($data['module'] as $key =&gt; $value) { 
            $res .= $value['route']; 
        }     

        $this-&gt;layout-&gt;setMenu($res);

    }


And exactly the same in the Support Controller:

class Support extends Ci_Controller {

    private $user; 
    private $permiso; 
    private $order;

    function __construct(){

        parent::__construct();

        if (!$this-&gt;session-&gt;userdata('session_user')) {
            $this-&gt;session-&gt;set_flashdata("mensaje", 'No cuenta con permisos *');
            redirect();
            $this-&gt;session_usuario = 1;
        }

        $session_data = $this-&gt;session-&gt;userdata('session_user');
        $image        = $session_data['img'];
        $nickname     = $session_data['nick'];
        $res          = "";

        $this-&gt;layout-&gt;setLayout('layout/admin');
        $this-&gt;layout-&gt;setImage(base_url().$image);
        $this-&gt;layout-&gt;setNick($nickname);

        $data['module'] = $this-&gt;usuarios_model-&gt;seleccionarModulos($session_data['perfil'] );

        for ($i=0; $i &lt; count($data['module']) ; $i++) {                                                    
            $info['functionality'] = $this-&gt;usuarios_model-&gt;seleccionarFuncionalidades(     $session_data['usuario'], $data['module'][$i]['id']);
            $data['module'][$i]['route'] = $this-&gt;load-&gt;view($data['module'][$i]['route'], $info, true);
        }

        foreach ($data['module'] as $key =&gt; $value) { 
            $res .= $value['route']; 
        }     

        $this-&gt;layout-&gt;setMenu($res);

    }


I know that it's a wrong way to solve this problem, and I want to improve it and reuse the code.
You need to extend CI_Controller with your code and extend your classses from your custom class, not default. I'll try to explain:

At first create MY_Controller.php at /application/core/ with next content:

     &lt;?php

     class MY_Controller extends CI_Controller {
              function __construct() {
                      parent::__construct();
                          // Your checks here
              }
     }

     ?&gt;


Now edit your controllers like this:

was

class Support extends Ci_Controller {


will be

class Support extends MY_Controller {


Now your controller will use constructor from your MY_Controller and still use CodeIgniter's default controller class.

You can read more from the manual.