This Laravel/Lumen package provides additional events for the Illuminate Database package. Currently, this package adds:
- a
DatabaseConnecting
event that is fired before connecting to the database, which can modify the configuration or cancel the connection - a
DatabaseConnected
event that is fired after connecting to the database - a
ConnectingException
runtime exception that is thrown if the database connection is cancelled by theDatabaseConnecting
event
Additional events may be added be added as requested/submitted.
Via Composer
$ composer require shiftonelabs/laravel-db-events
Once composer has been updated and the package has been installed, the service provider will need to be loaded.
For Laravel 4, open app/config/app.php
and add following line to the providers array:
'ShiftOneLabs\LaravelDbEvents\LaravelDbEventsServiceProvider',
For Laravel 5, open config/app.php
and add following line to the providers array:
ShiftOneLabs\LaravelDbEvents\LaravelDbEventsServiceProvider::class,
For Lumen 5, open bootstrap/app.php
and add following line under the "Register Service Providers" section:
$app->register(ShiftOneLabs\LaravelDbEvents\LaravelDbEventsServiceProvider::class);
The ShiftOneLabs\LaravelDbEvents\Extension\Database\Events\DatabaseConnecting
event allows you to hook into the database connection lifecycle before the connection is established. Additionally, this event provides you the ability to modify the configuration used for the connection, as well as completely cancel the connection attempt.
Attributes
The DatabaseConnecting
event provides three public attributes:
Attribute | Description |
---|---|
public $connector |
The Connector object making the connection. This package extends each of the built-in connectors. |
public $connectionName |
The name of the selected database connection configuration. |
public $config |
The configuration array used to connect to the database. |
Example:
app('events')->listen('ShiftOneLabs\LaravelDbEvents\Extension\Database\Events\DatabaseConnecting', function ($event) {
app('log')->info('Connector class: '.get_class($event->connector));
app('log')->info('Connection name: '.$event->connectionName);
app('log')->info('Configuration: '.print_r($event->config, true));
});
Modifying Connection Configuration
The configuration for your database connections is usually stored in your config/database.php
file (in conjunction with your .env
file). If, however, you need to dynamically modify the configuration used for the connection, this can be done inside a DatabaseConnecting
event listener. Any changes made to the configuration in the event listener will be used for the database connection.
Example:
app('events')->listen('ShiftOneLabs\LaravelDbEvents\Extension\Database\Events\DatabaseConnecting', function ($event) {
// don't connect to mysql in strict mode if you like zeroed out dates
if (i_like_zero_dates()) {
$event->config['strict'] = false;
}
});
Cancelling the Connection
There may be situations where you would like to prevent the database from attempting the connection. In this case, the database connection attempt can be cancelled by returning false
from a DatabaseConnecting
event listener. If the database connection is cancelled, a ShiftOneLabs\LaravelDbEvents\Exceptions\ConnectingException
runtime exception will be thrown.
Example:
app('events')->listen('ShiftOneLabs\LaravelDbEvents\Extension\Database\Events\DatabaseConnecting', function ($event) {
if (not_todaaay()) {
return false;
}
});
The ShiftOneLabs\LaravelDbEvents\Extension\Database\Events\DatabaseConnected
event allows you to hook into the database connection lifecycle after the connection is established. Additionally, this event provides access to the final configuration used for the connection, as well as the connection itself.
Attributes
The DatabaseConnected
event provides four public attributes:
Attribute | Description |
---|---|
public $connector |
The Connector object making the connection. This package extends each of the built-in connectors. |
public $connectionName |
The name of the selected database connection configuration. |
public $config |
The configuration array that was used to connect to the database. |
public $pdo |
The connected PDO (or potentially Doctrine\DBAL\Driver\PDOConnection , as of 5.3) object. |
Example:
app('events')->listen('ShiftOneLabs\LaravelDbEvents\Extension\Database\Events\DatabaseConnected', function ($event) {
app('log')->info('Connector class: '.get_class($event->connector));
app('log')->info('Connection name: '.$event->connectionName);
app('log')->info('Configuration: '.print_r($event->config, true));
app('log')->info('PDO class: '.get_class($event->pdo));
});
Contributions are very welcome. Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.