HMVC comes with great features. But using it with codeigniter is a bit problematic, Today we will see how we can cross load library of a module into another module. Suppose we are using the DX_Auth library. And we have a module named 'Auth' and paste all the necessary file within it. We want to use it outside of all the modules, in the main controller folder. We can use the following command:
$this->load->model('module/controller');
$this->load->module('module/controller');
But with DX_auth there would be a problem as it also need to have the libraries and config file to be loaded. For that purpose we will use a autoload file under config folder in our module. HMVC now autoloads a variable name $autoload. We can call it in the construct funtion, but then it will be called only once. The autoload file under config folder of auth module would be like this:
$autoload['libraries'] = array('DX_Auth_Event');
/*
| -------------------------------------------------------------------
| Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['helper'] = array('url', 'file');
*/
$autoload['helper'] = array('url','form');
/*
| -------------------------------------------------------------------
| Auto-load Plugins
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['plugin'] = array('captcha', 'js_calendar');
*/
$autoload['plugin'] = array();
/*
| -------------------------------------------------------------------
| Auto-load Config files
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['config'] = array('config1', 'config2');
|
| NOTE: This item is intended for use ONLY if you have created custom
| config files. Otherwise, leave it blank.
|
*/
$autoload['config'] = array('dx_auth');
/*
| -------------------------------------------------------------------
| Auto-load Language files
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['language'] = array('lang1', 'lang2');
|
| NOTE: Do not include the "_lang" part of your file. For example
| "codeigniter_lang.php" would be referenced as array('codeigniter');
|
*/
$autoload['language'] = array();
/*
| -------------------------------------------------------------------
| Auto-load Models
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['model'] = array('model1', 'model2');
|
*/
$autoload['model'] = array();
And now we will create our necessary function within the auth controller or what ever controller you want to write in. suppose we write the following function:
function is_logged_in()
{
$this->load->library('DX_Auth');
// Set auth info
if($this->dx_auth->is_logged_in())
{
$this->userId = $this->dx_auth->get_user_id();
$this->_data['userId'] = $this->userId;
}
}
Now we will call this function from our main root controller folder, like this :
$this->load->module('auth');
$this->auth->is_logged_in();
See the book
OpenCart 1.4 Template Design Cookbook.
See the book
Joomla Mobile Development Beginners Guide