2013-10-28 129 views
0

我想每次在CodeIgniter中加載一個頁面時實例化一個類。CodeIgniter自動加載包

它看起來像/application/config/autoload.php是做這個的地方。那是對的嗎?

我加入這行來包裝的自動加載:

$autoload['packages'] = array('/application/third_party/Autoload.php'); 

現在我需要這個代碼中的每一頁上執行,在那裏我可以做到這一點?

$bugsnag = new Bugsnag_Client("YOUR-API-KEY-HERE"); 
set_error_handler(array($bugsnag, "errorHandler")); 
set_exception_handler(array($bugsnag, "exceptionHandler")); 

回答

1

那麼讓我來解釋一下你如何做到這一點。
由於您已經自動加載包,現在您需要執行此操作。
在application/core /目錄下創建一個MY_Controller。

Class MY_Controller Extends CI_Controller{ 

    public $bugsnag = ''; 
    public 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"));  
    } 
} 

注意$this->bugsnag現在包含該對象。當你需要訪問它在任何頁面,您可以簡單地做這樣通過擴展父類

Class Test Extends MY_Controller{ 

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

    public function index(){ 
     echo '<pre>'; 
     print_R($this->bugsnag); 
    } 
} 
0

創建一個MY_Controller &繼承你的所有控制器。你可以通過谷歌搜索找到更多關於「MY_Controller」的信息

3

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

以下是MY_Controller的一個版本。PHP 這是通過使用作曲家安裝BugSnag

使用這種方法的$this->_bugsnag變量暴露給整個CI系統

class MY_Controller extends CI_Controller { 

    // Application Version 
    public $_app_version; 

    // Bugsnag 
    public $_bugsnag = NULL; 

    /** 
    * Constructor 
    */ 
    public function __construct() { 

     parent::__construct(); 

     // Dont print errors to screen 
     ini_set('display_errors', 0); 

     // Load configs 
     $this->load->config('appversion'); 
     $this->load->config('bugsnag'); 
     $this->_app_version = $this->config->item('app_version'); 

     // INIT: bugsnag 
     // https://docs.bugsnag.com/platforms/php/other/configuration-options/ 
     $this->_bugsnag = Bugsnag\Client::make($this->config->item('bugsnagAPIKey')); 
     $this->_bugsnag->setErrorReportingLevel(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED); 
     $this->_bugsnag->setNotifyReleaseStages(['developement', 'testing', 'production']); 
     $this->_bugsnag->setReleaseStage(ENVIRONMENT); 
     $this->_bugsnag->setAppType('API Server'); 
     $this->_bugsnag->setAppVersion($this->_app_version); 
     $this->_bugsnag->setHostname($_SERVER['HTTP_HOST']); 
     $this->_bugsnag->setProjectRoot(realpath(APPPATH)); 
     $this->_bugsnag->setFilters(['password']); 
     Bugsnag\Handler::register($this->_bugsnag); 

     // Load Helpers 

     // Load Libraries 

     // Load Languages 
    } 
} 

您現在可以訪問BugSnag方法是這樣的。

$this->_bugsnag->leaveBreadcrumb('Hello'); 
$this->_bugsnag->notifyException($e);