Friday, June 25, 2010

Resolving HMVC problem in Codeigniter

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




List of my works:

Technical Support:

If you still face the technical problem, please get support of our highly skilled technical team: garazlab.com.


Wordpress Plugins:
  1. Real-Time Health Data from Every Where:WP plugin to display real-time health data & increase sale by promoting user specific products according to health information: garazlab.com.
  2. Woocommerce Stock Notification Builder:Sends desktop, mobile & email notifications with full customization.Build your own product notification system with it: garazlab.com.

Opencart Extensions:

  1. Product Based Quantity Wise Shipping: Find it here.
  2. OpenSSLCOMMERZ: integrate SSLCOMMERZ with opencart: Find it here.
  3. Fine Search v.1.0 - Improves Opencart search feature to find relevant: Find it here.
  4. Opensweetcaptcha - An easy way to generate attractive captcha for your system!: Find it here.
  5. Custom Field Product - add unlimited custom fields to the product form: Find it here.
  6. Formcaptcha - add captcha on the register page: Find it here.

My Books:

  1. OpenCart 1.4 Template Design Cookbook.
  2. Joomla Mobile Development Beginners Guide

4 comments:

Anonymous said...

I shouldn't go on reading after
---
$this->load->model('module/controller')
---
Sorry, in the current state articles makes very little sense. You seem to describe a distinct situation that happened in your code, but neither give a good description, nor make an understandable generalization.

Complete listings of modules would help, I suppose.

Unknown said...

What I have told in my post is that how we can cross load our modules with each component of it in hmvc. I have added the config file here also. and there is every description of the problem and how it could be fixed.

Riddict said...

what is H meaning in HMVC?
Earli.us

Unknown said...

H means hierarchical.