Install via Composer:
composer require bugsnag/bugsnag-symfony
Register the Bugsnag\Symfony\BugsnagBundle in config/bundles.php (Symfony) or use the Laravel service provider if applicable. Configure via .env or config/services.php:
BUGSNAG_API_KEY=your_api_key
BUGSNAG_ENDPOINT=https://notify.bugsnag.com
For Laravel, publish the config:
php artisan vendor:publish --provider="Bugsnag\Symfony\BugsnagBundle\BugsnagBundle" --tag="config"
First use case: Automatic error reporting. Install the bundle, configure your API key, and let Bugsnag capture exceptions automatically. Verify by triggering an unhandled exception (e.g., 1/0 in a route).
Bundle Registration:
Add to config/bundles.php:
Bugsnag\Symfony\BugsnagBundle\BugsnagBundle::class => ['all' => true],
For Symfony 5.4+, leverage autowiring to inject BugsnagClient into services:
use Bugsnag\Client;
class MyService {
public function __construct(private Client $bugsnag) {}
}
Manual Error Reporting: Explicitly notify Bugsnag for critical failures:
$this->bugsnag->notify(new \RuntimeException('Custom error message'));
Contextual Data: Attach metadata (e.g., user ID, request data) to reports:
$this->bugsnag->notify($exception)
->addTab('Request', ['data' => $request->all()]);
Service Provider:
Bind the Bugsnag client in AppServiceProvider:
$this->app->singleton(\Bugsnag\Client::class, function ($app) {
return new \Bugsnag\Client(config('bugsnag.api_key'));
});
Exception Handling:
Extend Laravel’s App\Exceptions\Handler to notify Bugsnag:
public function report(Throwable $exception) {
if (app()->bound(\Bugsnag\Client::class)) {
app(\Bugsnag\Client::class)->notify($exception);
}
parent::report($exception);
}
Middleware for API Errors: Use middleware to catch API-specific errors:
use Bugsnag\Client;
class BugsnagMiddleware {
public function handle($request, Closure $next, Client $bugsnag) {
try {
return $next($request);
} catch (\Throwable $e) {
$bugsnag->notify($e)->addTab('API Request', $request->all());
throw $e;
}
}
}
PHP 7.2+ Requirement:
php -v to check your version. Update via pecl upgrade or your system package manager.Symfony 5.4+ Requirement:
v1.x of the package or upgrade Symfony:
composer require symfony/*:^5.4
Disable Auto-Notification:
Temporarily disable Bugsnag in config/services.php:
'bugsnag' => [
'enabled' => env('BUGSNAG_ENABLED', false),
],
Test Locally:
Use the BUGSNAG_ENDPOINT to point to a local mock server during development:
BUGSNAG_ENDPOINT=http://localhost:3000/notify
Check Ignored Exceptions:
Configure config/bugsnag.php to ignore specific exceptions (e.g., HttpException):
'ignore_exceptions' => [
\Symfony\Component\HttpKernel\Exception\HttpException::class,
],
Custom Error Formatting:
Override the Bugsnag\Symfony\BugsnagBundle\EventListener\ExceptionListener to modify payloads:
$listener->setBugsnagClient($customClient);
Symfony 8 Features: Leverage Symfony 8’s improved dependency injection:
# config/services.yaml
Bugsnag\Client: '@bugsnag.client'
Laravel Scout Integration: For Laravel, pair with Scout to track model-level errors:
try {
Model::scout()->search('query');
} catch (\Exception $e) {
$bugsnag->notify($e)->addTab('Scout Query', ['query' => 'search']);
}
How can I help you explore Laravel packages today?