Many of us are using the HMVC extension (Hierarchical Model View Controller) in codeigniter which is available. http://codeigniter.com/wiki/Modular_Extensions_-_HMVC
HMVC helps developing modular apps in a very convenient way. All the things work simply fine just like a clean CI installation. There’re 1 or two exceptions which i have found. One is the callback of Form_validation class. If we are making some forms like registration we check emails and usernames and we do it in the form validation class using callbacks. If we look at the basic callback structure when validating forms from CI documentation, it appears like.
< ?php class Form extends Controller { function index() { $this->load->helper( array ( 'form', 'url' ) ); $this->load->library( 'form_validation' ); $this->form_validation->set_rules( 'username', 'Username', 'callback_username_check' ); $this->form_validation->set_rules( 'password', 'Password', 'required' ); $this->form_validation->set_rules( 'passconf', 'Password Confirmation', 'required' ); $this->form_validation->set_rules( 'email', 'Email', 'required' ); if ($this->form_validation->run() == FALSE) { $this->load->view( 'myform' ); } else { $this->load->view( 'formsuccess' ); } } function username_check($str) { if ($str == 'test') { $this->form_validation->set_message( 'username_check', 'The %s field can not be the word "test"' ); return FALSE; } else { return TRUE; } } } ?>
But this callback will not simply work. Some of my precious hours went why it was not working. Then i navigated the Form_validation.php in /system/libraries folder around line 580 which looks like
if ($callback === TRUE) { if ( ! method_exists($this->CI, $rule)) { continue; }
The function – method_exists returns false even if the callback function is there in the Controller and after all in the CI loader object. So it came out from the forum that you have to do a little extension to Form Validation class which is
< ?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class MY_Form_validation extends CI_Form_validation { function run($module = '', $group = '') { (is_object($module)) AND $this->CI =& $module; return parent::run($group); } } /* End of file MY_Form_validation.php */ /* Location: ./application/libraries/MY_Form_validation.php */
And when running the validation like in earlier code
if ($this->form_validation->run() == FALSE)
you have to provide an addition $this as a parameter to run method. which will look like
if ($this->form_validation->run($this) == FALSE)
Now, your validation callbacks will work perfectly.
Another note, if you’re trying to access a public variable of a controller via the get_instance() in hooks , you’ll have to write in your controller like this.
CI::instance()->var_name = “Some Value”;
May be there are some other required adjustments when using HMVC, but i came across this two time killing adjustments.