CodeIgniter 3

note: This is the documentation for version 3 of CodeIgniter. See the documentation to integrate Raygun into a CodeIgniter 4 app here.

Installation

Use Composer to add the Raygun4PHP package to your CodeIgniter 3 project. Run the following command in a shell at the root of your project:

composer require "mindscape/raygun4php:2.*"

Inside of /application/config/config.php ensure that enable_hooks is set to true:

$config['enable_hooks'] = TRUE;

Create a file at /application/hooks/RaygunSetup.php. This file is going to create the RaygunClient and setup the error/exception handlers.

Add the following content to that new file. Making sure to substitute the <strong>apiKey</strong>.

&lt;?php
use GuzzleHttp\Client;
use Raygun4php\RaygunClient;
use Raygun4php\Transports\GuzzleAsync;
use Raygun4php\Transports\GuzzleSync;

require_once &#39;vendor/autoload.php&#39;;

class RaygunSetup {
    private static $raygunClient;

    public function __construct() {
        $httpClient = new Client([
            &#39;base_uri&#39; =&gt; &#39;https://api.raygun.com&#39;,
            &#39;headers&#39; =&gt; [
                &#39;X-ApiKey&#39; =&gt; &#39;paste_your_api_key_here&#39;
            ]
        ]);

        $transport = new GuzzleAsync($httpClient);

        self::$raygunClient = new RaygunClient($transport);

        register_shutdown_function([$transport, &#39;wait&#39;]);
    }

    public function set_exception_handler() {
        set_exception_handler( array( $this,&#39;exception_handler&#39; ) );
        set_error_handler( array( $this, &#34;error_handler&#34; ) );
    }

    function exception_handler($exception) {
        echo &#39;Exception thrown&#39;;
        self::$raygunClient-&gt;SendException($exception);
    }

    function error_handler( $errno, $errstr, $errfile, $errline) {
        self::$raygunClient-&gt;SendError($errno, $errstr, $errfile, $errline);
    }
}

In application/config/hooks.php wire up the pre_controller hook to the above RaygunSetup.

$hook[&#39;pre_controller&#39;] = array(
    &#39;class&#39;    =&gt; &#39;RaygunSetup&#39;,
    &#39;function&#39; =&gt; &#39;set_exception_handler&#39;,
    &#39;filename&#39; =&gt; &#39;RaygunSetup.php&#39;,
    &#39;filepath&#39; =&gt; &#39;hooks&#39;
);

Deploy Raygun into your production environment for best results, or raise a test exception. Once we detect your first error event, the Raygun app will automatically update.


The provider is open source and available at the Raygun4PHP repository.