要自動負載(根據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"));
}
}