要自动负载(根据CI
)一个包,你应该把package path/name
以下数组中,喜欢
$autoload['packages'] = array(APPPATH.'third_party', '/usr/local/shared');
但它不会自动执行任何代码,但会使您的包无需显式加载即可使用。
为了每次都执行一些代码,您可以将该代码放入您的基本控制器的constructor
函数中。另外,您可以将代码放入您的config.php
文件中。如果你有一个扩展的基本控制器,如application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
//
}
然后你可以使用它的构造函数一样
class MY_Controller extends CI_Controller {
function __construct()
{
parent::__construct();
$this->bugsnag = new Bugsnag_Client("YOUR-API-KEY-HERE");
set_error_handler(array($bugsnag, "errorHandler"));
set_exception_handler(array($bugsnag, "exceptionHandler"));
}
}
Rest的控制器将使用/延长MY_Controller
而不是CI_Controller
。
但你也可以在这种情况下使用hook
(注册自定义异常处理程序),在application/config/hooks.php
文件,把下面的代码
$hook['pre_controller'][] = array(
'class' => 'CustomExceptionHook',
'function' => 'SetExceptionHandlers',
'filename' => 'CustomExceptionHook.php',
'filepath' => 'hooks'
);
创建application/hooks/CustomExceptionHook.php
文件夹中的一类,像
class CustomExceptionHook
{
public function SetExceptionHandlers()
{
// add package path (if not auto-loaded)
$this->load->add_package_path(APPPATH.'third_party/package_folder/');
// load package (if not auto-loaded)
$this->load->library('Bugsnag_Client');
set_error_handler(array($this->Bugsnag_Client, "errorHandler"));
set_exception_handler(array($this->Bugsnag_Client, "exceptionHandler"));
}
}