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 apiKey.

<?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([
            'base_uri' => 'https://api.raygun.com',
            'headers' => [
                'X-ApiKey' => 'paste_your_api_key_here'
            ]
        ]);

        $transport = new GuzzleAsync($httpClient);

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

        register_shutdown_function([$transport, 'wait']);
    }

    public function set_exception_handler() {
        set_exception_handler( array( $this,'exception_handler' ) );
        set_error_handler( array( $this, "error_handler" ) );
    }

    function exception_handler($exception) {
        self::$raygunClient->SendException($exception);
    }

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

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

Events::on('pre_system', [new RaygunSetup(), 'set_exception_handler']);

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.