如何在不扩展CI_Controller的情况下访问 CodeIgniter 函数


How to access CodeIgniter functions without extending CI_Controller

我正在尝试编写一个外部库,该库具有各种类中常用的函数。

目前我正在尝试编写日志消息函数。问题是我需要访问会话库和模型。如何在不从CI_Controller扩展的情况下访问它们?有什么解决方法吗?

这是我的代码:

Common_functions库:

public function _send_message($message, $log_to_db=TRUE)
{
     $this->session->set_userdata("message", $message);
     if($log_to_db) $this->User_log_model->log_mesage($message);
}

其他类中的用法示例:

public function new_user()
{
    $this->_set_validation_rules();
    if($this->form_validation->run())
    {
        if($user_id = $this->User_model->insert($this->_prepare_new_user_array()))
        {
            $this->common_functions->_send_message("New User created successfully. | user_id: " . $user_id);
        }
        else {
            $this->common_functions->_send_message("Unable to create new User record.");
        }
    }
}
设法

解决了它。只是将我的日志消息函数移到了User_log_model

相关文章: