The problem:
I’ve defined a few constants in my hook but I can’t access them inside my sub-classed controller constructor.
The code:
A – the hook class:
class Settings extends CI_Hooks {
public function load_settings() {
$CI =& get_instance();
$CI->load->model('hooks/settings_model');
$data = $CI->settings_model->load_settings();
define('MEMBERS_PER_PAGE', $data['members_per_page']);
define('REGISTER_ENABLED', $data['register']);
define('SITE_ACCESS_ENABLED', $data['site_access']);
define('ADMIN_EMAIL', $data['admin_email']);
}
}
B – the hook config:
$hook['post_controller_constructor'] = array(
'class' => 'settings',
'function' => 'load_settings',
'filename' => 'settings.php',
'filepath' => 'hooks'
);
C – the controller
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
defined('SITE_ACCESS_ENABLED') ? print SITE_ACCESS_ENABLED : print "NULL";
}
}
The way I understand *post_controller_constructor* is that it loads after the controller is initialized but before the constructor is executed. Apparently my defined constants don’t work in any constructor while constants from config/constants.php do work.
Any help and insights are greatly appreciated as hooks are totally new to me.
Well, post_controller_constructor happens just then. After the constructor has finished constructing the controller :-).
You need to have it fire at
pre_controllerand manage the instantiation of the model on your own, or you will have to wait until the controller’s method is called before you can access the values. Sorry.