Reputation: 5089
I have extended my CI_Controller like this:
// base extend
class MY_Controller extends CI_Controller {
public $CI = array();
public function __construct() {
parent::__construct();
$this->CI = & get_instance();
}
public function isUser(){
// for example
}
}
// admin extended
class MY_AdminController extends MY_Controller {
public $admin = array();
public function __construct() {
parent::__construct();
$this->CI->lang->load('admin');
$this->admin['lang'] = $this->CI->lang->line('admin');
$this->CI->load->vars($this->admin);
}
public function isAdmin(){
//for example
}
}
// extends for modules
class MY_AdminModuleController extends MY_AdminController {
public function __construct() {
parent::__construct();
$this->CI->load->view('_header');
}
public function isAllowedModule(){
// example
}
public function pseudoDestruct(){
$this->CI->load->view('_footer');
}
}
So it works fine. But I try to hook post_controller
event and add my MY_AdminModuleController->pseudoDestruct()
, so I enabled hooks in config.php and added next lines to hooks:
$hook['post_controller'] = array(
'class' => 'MY_AdminModuleController',
'function' => 'pseudoDestruct',
'filename' => 'MY_Controller.php',
'filepath' => 'core'
);
But I got a problem at loading lang-file in MY_AdminController's constructor. It returns null
when called from hook (true
when I use it normaly) and I have Notice about undefined index at frontend. No, I don't want to disable notices, I want to fix the problem. Also I have config loadings in MY_AdminController's constructor and them loading good.
Upvotes: 0
Views: 1086
Reputation: 14752
You can't do that, not in that particular way at least. CodeIgniter is designed to only have one controller instance, while the hooks create new instances and your lang files are not loaded in that new instance.
Also, you don't need to call get_instance()
from your controller class - the class IS what get_instance()
returns.
Anyway, you can declare a regular function to use as a hook, there's no problem putting it in your MY_Controller.php file as well:
function pseudo_destruct()
{
get_instance()->load->view('_footer');
}
Then use this hook:
$hook['post_controller'] = array(
'function' => 'pseudo_destruct',
'filename' => 'MY_Controller.php',
'filepath' => 'core'
);
Upvotes: 1