I'm experiencing a problem using the HMVC implementation of Codeigniter, with modules.
I created an authentication module, called autenticacion, which, for obvious reasons, in order to check session validity and expiration, is always used within the MX_Controller, this way:
Modules::run('autenticacion/logout', $user, true);
or
Modules::run('autenticacion/update', $user);
depending on certain conditions.
Ever since I added this athentication implementation, the normal access to other modules was broken.
Now if I try to, for instance, access
www.domain.com/items
having that I coded this module files:
modules/items/controllers/Items.php
(this extends MX_Controller)
, and the view:
modules/items/views/items_view.php
The view won't be found despite it's loaded within the Items controller.
If I print out $this in the Items constructor, the MY_Loader instance displays this property:
[_module:protected] => autenticacion
I understand this means the items module is not being loaded within the loader, despite I can reach its controller. The other module (autenticacion) seems that is messing it all.
How could I fix this?
EDIT:
This is what I changed in MX_Controller in order to handle session checks and updates:
<?
class MX_Controller
{
public $autoload = array();
public function __construct()
{
// Validación de la autenticación/caducidad de la sesión
$this->_validar_sesion();
// Original actions
//....
}
//... method __get()
/**
* Checks whether the user has not yet created a valid authenticated user session, or if it has expired.
* In both cases, it redirects to the authentication page, deleting the expired session if it was already created.
*/
private function _validar_sesion()
{
if (stristr(get_class($this), 'autenticacion')) return;
require_once APPPATH . 'third_party/usuarios/Usuario.php';
$this->load->model('autenticacion/autenticacion_model');
// Get user instance from session
$usuario = unserialize($this->session->userdata('usuario'));
// No authenticated session yet: redirecting to the authentication page
if (!stristr(current_url(), 'autenticacion') && ! $this->session->logged_in) {
$this->load->library('uri');
$uri = $this->uri->uri_string();
redirect(base_url() . 'autenticacion' . ($uri ? '?redirect=' . $uri : ''));
}
// There is already an authenticated session, and the request is not coming from the authentication page
elseif (!stristr(current_url(), 'autenticacion')) {
// Check session expiration, in which case, we do logout
if ($this->autenticacion_model->sesion_caducada($usuario, config_item('sess_companyname_expiration'))) {
// This will delete the session from DB, destroy it, and redirect to the authentication page
Modules::run('autenticacion/logout', $usuario, true);
}
// Session has not expired yet: we update the session timestamp in DB to extend the expiration time
else {
Modules::run('autenticacion/update', $usuario);
}
}
}
}