One of the best features of the Laravel framework is the ability to extend or change it’s core components. And to make it even more flexible, Laravel also includes a simple observer implementation where you can subscribe to and listen for events that occur in your application. Today, I’m going to describe a few of the events that are triggered by Laravel, but you can find a complete list at the bottom of this article. Note that Laravel framework event names may differ for each version — I’ll be discussing Laravel 5.3 events in this article.
<?XML:NAMESPACE PREFIX = «[default] http://www.w3.org/2000/svg» NS = «http://www.w3.org/2000/svg» /> cache:cleared
This event name describes itself — it is fired after the cache has been cleared and only if you use Artisan command cache:clear
. Keep in mind that this event is not fired if you clear the cache programmatically with Cache::forget('key')
or Cache::flush()
.
php artisan cache:clear memcached --tags=tagName
Event::listen('cache:cleared', function ($store, $tags) {
// $store => "memcached"
// $tags => ["tagName"]
});
illuminate.log
This event enables you to listen for application logs. However, it does not observe exceptions because the Laravel exception handler directly calls Psr\Log\LoggerInterface->error
instance (see \App\Exceptions\Handler
) which means that only messages logged by Laravel \Log
facade (or via the IOC container) will be available.
If you’re interested in listening for all of your application logs then it will be necessary to create a Monolog handler and bind it to Psr\Log\LoggerInterface
. See the Monolog documentation if you would like to know more about this.
Log::info('My awesome log message', ['key' => 'value']);
Event::listen('illuminate.log', function ($level, $message, $context) {
// $level => "info"
// $message => "My awesome log message"
// $context => ["key" => "value"]
});
kernel.handled
This event is fired right after an object is received from the response handler and before it’s returned (to the browser or console).
Event::listen('kernel.handled', function ($request, $response) {
// $request => Illuminate\Http\Request
// $response => Illuminate\Http\Response
});
\Illuminate\Queue\Events\JobProcessed::class
This event could be useful if you would like to store all processed queue jobs in a MySQL table. All necessary data is available within the event object, including the job payload.
Event::listen(\Illuminate\Queue\Events\JobProcessed::class, function ($event) {
// $event->job => Illuminate\Queue\Jobs\SyncJob
// $event->connectionName => "sync"
});
Final thoughts
Events allow you to observe your framework state at specific points, and can be put to use in a variety of scenarios. Hopefully, this article will help give you some insight into what’s available under the hood.
Links to Resources
- https://laravel.com/docs/5.3/events
- https://github.com/Seldaek/monolog/blob/master/doc/02-handlers-formatters-processors.md
Complete list of Laravel 5.3 core events:
Illuminate\Auth\Events\Attempting
Illuminate\Auth\Events\Failed
Illuminate\Auth\Events\Login
Illuminate\Auth\Events\Authenticated
Illuminate\Auth\Events\Logout
cache:clearing
cache:cleared
Illuminate\Cache\Events\CacheHit
Illuminate\Cache\Events\CacheMissed
Illuminate\Cache\Events\KeyForgotten
Illuminate\Cache\Events\KeyWritten
Illuminate\Console\Events\ArtisanStarting
Illuminate\Database\Events\QueryExecuted
Illuminate\Database\Events\TransactionBeginning
Illuminate\Database\Events\TransactionCommitted
Illuminate\Database\Events\TransactionRolledBack
bootstrapping: *
bootstrapped: *
locale.changed
kernel.handled
illuminate.log
Illuminate\Mail\Events\MessageSending
Illuminate\Notifications\Events\BroadcastNotificationCreated
Illuminate\Queue\Events\JobProcessing
Illuminate\Queue\Events\JobProcessed
Illuminate\Queue\Events\JobExceptionOccurred
Illuminate\Queue\Events\JobFailed
Illuminate\Routing\Events\RouteMatched
illuminate.queue.looping
composing: *
creating: *
eloquent.booting
eloquent.booted
eloquent.deleting
eloquent.deleted
eloquent.saving
eloquent.saved
eloquent.updating
eloquent.updated
eloquent.creating
eloquent.created
eloquent.restoring
eloquent.restored