简体   繁体   English

CodeIgniter自动加载软件包

[英]CodeIgniter autoload a package

I want to instantiate a class every time a page is loaded in CodeIgniter. 我想在每次在CodeIgniter中加载页面时实例化一个类。

It looks like the /application/config/autoload.php is the place to do this. 看起来/application/config/autoload.php是执行此操作的地方。 Is that correct? 那是对的吗?

I added this line to the package's autoload: 我将此行添加到软件包的自动加载中:

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

Now I need this code to be executed on every page, where can I make this happen? 现在,我需要在每个页面上执行此代码,在哪里可以实现?

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

To auto load a package (according to CI ), you should put the package path/name in following array, like 要自动加载软件包(根据CI ),应将package path/name放在以下数组中,例如

$autoload['packages'] = array(APPPATH.'third_party', '/usr/local/shared');

But it doesn't execute any code automatically but makes your package available to use without explicitly loading it. 但是它不会自动执行任何代码,而是可以在不显式加载的情况下使用您的程序包。

To make some code execute every time, you can put that code in your base controller's constructor function. 要使每次执行一些代码,您可以将该代码放入基本控制器的constructor函数中。 Also, you can put the code in your config.php file. 另外,您可以将代码放在config.php文件中。 If you have an extended base controller, like application/core/MY_Controller.php 如果您有扩展的基本控制器,例如application/core/MY_Controller.php

class MY_Controller extends CI_Controller {
    //
}

Then you can use it's constructor function like 然后您可以使用它的构造函数,例如

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 of your controllers will use/extend MY_Controller instead of CI_Controller . 其余的控制器将使用/扩展MY_Controller而不是CI_Controller

But you can also use a hook in this case (to register custom exception handlers), in application/config/hooks.php file, put following code 但是在这种情况下,您也可以使用hook (注册自定义异常处理程序),在application/config/hooks.php文件中,放置以下代码

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

Create a class in application/hooks/CustomExceptionHook.php folder, like 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"));
    }
}

Well let me explain it how you can do it. 好吧,让我解释一下如何实现。
As you have autoloaded the package its fine now you need to do this. 自动加载软件包后,现在需要执行此操作。
Create a MY_Controller in application/core/ directory. 在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"));     
    }
}

Note $this->bugsnag contains the object now. 注意$this->bugsnag现在包含该对象。 When you need to access it in any page you can simply do it like this by extending the parent class 当您需要在任何页面中访问它时,只需扩展父类就可以做到这一点

Class Test Extends MY_Controller{

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

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

Here is a version of MY_Controller.php This is using BugSnag via composer install 这是MY_Controller.php的版本,它通过composer install使用BugSnag

Using this method exposes the $this->_bugsnag variable to the entire CI System 使用此方法$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
    }
}

You can now access the BugSnag methods like this. 您现在可以像这样访问BugSnag方法。

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

Create a MY_Controller & inherit all your controllers off that. 创建一个MY_Controller并继承所有的控制器。 You can find more on this by Googling "MY_Controller" 您可以通过谷歌搜索“ MY_Controller”找到更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM