CodeIgniter 4

Installation

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

composer require "mindscape/raygun4php:2.*"

Create a file at /app/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
namespace App\Hooks;

use GuzzleHttp\Client;
use Raygun4php\RaygunClient;
use Raygun4php\Transports\GuzzleAsync;
use Raygun4php\Transports\GuzzleSync;

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) {
        self::$raygunClient-&gt;SendException($exception);
    }

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

In app/Config/Events.php wire up the pre_system event to the above RaygunSetup.

Events::on(&#39;pre_system&#39;, [new RaygunSetup(), &#39;set_exception_handler&#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.