2
\$\begingroup\$

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->session->userdata('session_user')) {
            $this->session->set_flashdata("mensaje", 'No cuenta con permisos *');
            redirect();
            $this->session_usuario = 1;
        }

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

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

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

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

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

        $this->layout->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->session->userdata('session_user')) {
            $this->session->set_flashdata("mensaje", 'No cuenta con permisos *');
            redirect();
            $this->session_usuario = 1;
        }

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

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

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

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

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

        $this->layout->setMenu($res);

    }

I know that it's a wrong way to solve this problem, and I want to improve it and reuse the code.

\$\endgroup\$
2
\$\begingroup\$

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:

     <?php

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

     ?>

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.

\$\endgroup\$
  • \$\begingroup\$ I agree, put all the shared code into MY_Controller and you'll save a lot of code improving maintainability! \$\endgroup\$ – Chococroc May 24 '14 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.