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.