Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Codeigniter:如何在控制器的构造函数中加载钩子中定义的常量_Codeigniter_Php - Fatal编程技术网

Codeigniter:如何在控制器的构造函数中加载钩子中定义的常量

Codeigniter:如何在控制器的构造函数中加载钩子中定义的常量,codeigniter,php,Codeigniter,Php,问题: 我在钩子中定义了一些常量,但无法在子类控制器构造函数中访问它们 代码: A-钩子类: class Settings extends CI_Hooks { public function load_settings() { $CI =& get_instance(); $CI->load->model('hooks/settings_model'); $data = $CI->settings_model-

问题: 我在钩子中定义了一些常量,但无法在子类控制器构造函数中访问它们

代码: A-钩子类:

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-钩子配置:

$hook['post_controller_constructor'] = array(
                                'class'    => 'settings',
                                'function' => 'load_settings',
                                'filename' => 'settings.php',
                                'filepath' => 'hooks'
                                );
C-控制器

class MY_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        defined('SITE_ACCESS_ENABLED') ? print SITE_ACCESS_ENABLED : print "NULL";
    }
}
我对*post_controller_constructor*的理解是,它在控制器初始化之后但在构造函数执行之前加载。显然,我定义的常量在任何构造函数中都不起作用,而config/constants.php中的常量起作用


非常感谢您提供的任何帮助和见解,因为挂钩对我来说是全新的。

好吧,post\u controller\u构造函数就在那时发生。构造函数完成构造控制器之后:-)


您需要让它在
pre_controller
上启动,并自己管理模型的实例化,否则您必须等到调用控制器的方法后才能访问这些值。抱歉。

无需道歉,谢谢您的明确回答。我决定扔掉钩子,换个方式做。