Welcome Guest, Not a member yet? Register   Sign In
[Solved] Codeigniter: Load Library in a Core Class
#12

I did find one way to incorporate the CI email class. It uses the same basic approach you have used in loading PHPMailer.  This even loads and uses application/config/email.php . (It is assumed to be there which is dangerous but this is just a proof-of-concept.)

Code:
class MY_Log extends CI_Log
{
    protected $mail;

    public function __construct($config = array())
    {
        parent::__construct($config);
        
        require(APPPATH.'config/email.php'); // adds $config to this scope

        require_once(BASEPATH.'libraries/Email.php');
        $this->mail = new CI_Email($config);
    }

    public function write_log($level, $msg)
    {
        //log_message($level, $msg);
        parent::write_log($level, $msg);

        if($level == 'error' && ENVIRONMENT == 'production')
        {
                      //do email stuff
        }
    }

}

For this to work there is one small hack that MUST BE MADE to system/libraries/Email.php

The last line in the constructor must be removed (or commented out)

Code:
   //log_message('info', 'Email Class Initialized');

Otherwise a loop condition exists where that call (log_message) tries to load the Log class, which tries to instantiate the Email class and... around and around we go.

So, there's your CI email class. Hope you don't want to load any other core libraries.
Reply


Messages In This Thread
RE: Codeigniter: Load Library in a Core Class - by dave friend - 11-03-2016, 06:44 PM



Theme © iAndrew 2016 - Forum software by © MyBB