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
Step 1 - Add Raygun4PHP
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.*"
Step 2 - Enable hooks
Inside of /application/config/config.php
ensure that enable_hooks
is set to true:
$config['enable_hooks'] = TRUE;
Step 3 - Create the RaygunSetup File
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 apiKey.
<?php
use GuzzleHttp\Client;
use Raygun4php\RaygunClient;
use Raygun4php\Transports\GuzzleAsync;
use Raygun4php\Transports\GuzzleSync;
require_once 'vendor/autoload.php';
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) {
echo 'Exception thrown';
self::$raygunClient->SendException($exception);
}
function error_handler( $errno, $errstr, $errfile, $errline) {
self::$raygunClient->SendError($errno, $errstr, $errfile, $errline);
}
}
Step 4 - Attach the hook
In application/config/hooks.php
wire up the pre_controller
hook to the above RaygunSetup.
$hook['pre_controller'] = array(
'class' => 'RaygunSetup',
'function' => 'set_exception_handler',
'filename' => 'RaygunSetup.php',
'filepath' => 'hooks'
);
Step 5 - Release
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.