扩展了多个系统类的codeigniter

时间:2015-04-09 13:14:41

标签: codeigniter

我想在codeigniter上处理错误,所以我需要扩展类" CI_Controller"和" CI_Exceptions"所以如何实现它。类似下面的东西。如果可以在整个系统中以钩子或其他方式全局处理错误?

class Language extends CI_Controller , CI_Exceptions{

    public function __construct()
    {
        parent::__construct();
        parent::CI_Exceptions();

    }
}

1 个答案:

答案 0 :(得分:0)

如何使用钩子有一个很好的post。我从帖子中复制了样本,但我会去那里阅读解决方案,以便朝着正确的方向前进。

<强>挂钩

$hook['pre_controller'][] = array(
                       'class'    => 'ExceptionHook',
                       'function' => 'SetExceptionHandler',
                       'filename' => 'ExceptionHook.php',
                       'filepath' => 'hooks'
                      );

异常类

class MY_Exceptions extends CI_Exceptions {

    public function __construct()
    {

        parent::CI_Exceptions();
    }

        public function show_php_error($severity, $message, $filepath, $line)
        {   
        $severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];
        $filepath = str_replace("\\", "/", $filepath);

        // For safety reasons we do not show the full file path
        if (FALSE !== strpos($filepath, '/'))
        {
                $x = explode('/', $filepath);
            $filepath = $x[count($x)-2].'/'.end($x);
        }

        if (ob_get_level() > $this->ob_level + 1)
        {
            ob_end_flush(); 
        }
        ob_start();
        include(APPPATH.'errors/error_php'.EXT);
        $buffer = ob_get_contents();
        ob_end_clean();

        $msg = 'Severity: '.$severity.'  --> '.$message. ' '.$filepath.' '.$line;

            log_message('error', $msg , TRUE);

        mail('dev-mail@example.com', 'An Error Occurred', $msg, 'From: test@example.com');  

        }

    }