A Silex Service Provider for Raven

0.1.2 2014-04-15 09:19 UTC


A basic Silex service provider for PHP client for Sentry : getsentry/raven-php.


The recommended way to install silex-raven is through Composer. Just create a composer.json file and run the php composer.phar install command to install it:

    "require": {
        "v-six/silex-raven": "0.1.*"

Alternatively, you can download the silex-raven.zip file and extract it.


$app->register(new SilexRaven\RavenServiceProvider(),
        'raven.dsn' => 'http://public:secret@example.com/1',
        'raven.options' => array(
            'logger' => 'my-logger-name' // Set custom logger name
        'raven.handle' => array(
            'exceptions' => false, // Disable exceptions handler
            'errors' => true, // Enable errors handler
            'fatal_errors' => true, // Enable fatal_errors handler

If necessary, set your own options in raven.options (see Raven documentation). All handlers are registered by default, you can disable them by setting corresponding configuration input to false in raven.handle.


You can easily capture an error or an exception with the following :

// Capture an error
$app['raven']->captureMessage('Oops !');

// Capture an exception
$app['raven']->captureException(new \Exception('Oops !'));

// Capture an exception with additional debug data
$app['raven']->captureException(new \Exception('Oops !'),
        'extra' => array(
            'php_version' => phpversion()

Obviously you can also provide custom request context :

// Bind the logged in user
$app['raven']->user_context(array('email' => 'foo@example.com'));

// Tag the request with something interesting
$app['raven']->tags_context(array('interesting' => 'yes'));

// Provide a bit of additional context
$app['raven']->extra_context(array('happiness' => 'very'));

// Clean all previously provided context


Here is a full example coupled with Silex error handler (see Silex error handlers documentation) :

$app = new Silex\Application();
    new \SilexRaven\RavenServiceProvider(),
    ['raven.dsn' => 'http://public:secret@example.com/1']

$app->error(function (\Exception $e, $code) use($app, $user) {
    $app['raven']->user_context(array('email' => $user->email));

    return new Response("There is an error !");