Laravel
note: The following instructions apply to Laravel 8.x and above. For instructions setting up Raygun on Laravel 5.x and below, see the legacy installation instructions further down the page.
Installation
Step 1 - Install Raygun
The best way to install Raygun4PHP is to use the Composer CLI tool. Run the following command in your project folder to install it.
composer require mindscape/raygun4php
Step 2 - Confugre Raygun in Exception Handler
Paste the following code in your app/Exceptions/Handler.php
file inside of the Handler
class:
// Override the ExceptionHandler's report() function
public function report(Throwable $e)
{
// Configure Raygun
$httpClient = new Client([
'base_uri' => 'https://api.raygun.com',
'headers' => [
'X-ApiKey' => 'paste_your_api_key_here'
]
]);
$transport = new GuzzleAsync($httpClient);
$raygunClient = new RaygunClient($transport);
$raygunClient->SendException($e); // Send exception to Raygun
$transport->wait();
// Continue with the default Laravel error handling
parent::report($e);
}
This will send all unhandled PHP exceptions to Raygun. For more detailed configuration options of our Raygun4PHP provider, see our documentation here.
Note: Your app API key is provided for you in the set up instructions when you create a new application in your Raygun account, or can be viewed in the application settings.
Step 3 - 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.
Manually sending exceptions
The above instructions will set up the Raygun library to automatically detect and send all unhandled exceptions to the Raygun app. Sometimes, however, you may want to send exceptions manually, such as handled exceptions from within a try/catch block.
Here's an example of manually sending exceptions from a try/catch block:
try
{
// Do something here that might go wrong
}
catch (\Exception $e)
{
// This will call the report function you overrode in the ExceptionHandler class
report($e);
}
Legacy Laravel installation
Step 1 - Install laravel-raygun wrapper with Composer
Install the Raygun4PHP provider along side the laravel-raygun wrappers by using Composer. Installation instructions can be found at https://github.com/davibennun/laravel-raygun.
Step 2 - Add Raygun into your ExceptionHandler report method
Open the file app\Exceptions\Handler.php
and add the following line before the class declaration.
use Raygun;
Step 3 - Sending All Exceptions
Inside app\Exceptions\Handler.php
add following line into the report
method.
public function report(Exception $e)
{
// Send all unhandled exceptions:
Raygun::SendException($e);
parent::report($e);
}
The provider is open source and available at the Raygun4PHP repository.