So the way I see it is that a good Laravel application should be very model- and event-driven.
I have a Model called Article
. I wish to send email alerts when the following events happen:
- When an Article is created
- When an Article is updated
- When an Article is deleted
The docs say I can use Model Events and register them within the boot()
function of App\Providers\EventServiceProvider
.
But this is confusing me because...
- What happens when I add further models like
Comment
orAuthor
that need full sets of all their own Model Events? Will the singleboot()
function ofEventServiceProvider
just be absolutely huge? - What is the purpose of Laravel's 'other' Events? Why would I ever need to use them if realistically my events will only respond to Model CRUD actions?
I am a beginner at Laravel, having come from CodeIgniter, so trying to wrap my head around the proper Laravel way of doing things. Thanks for your advice!
I found this the cleanest way to do what you want.
1.- Create an observer for the model (ArticleObserver)
2.- Create a new ServiceProvider (ObserversServiceProvider), remember to add it to you config/app.php
In your case, you may also use following approach:
Also, you need to register listeners in
App\Providers\EventServiceProvider
:Also make sure you have created the handlers in
App\Handlers\Events
folder/directory to handle that event. For example,article.created
handler could be like this:You've tagged this question as Laravel 5, so I would suggest not using model events as you'll end up with lots of extra code in your models which may make things difficult to manage in future. Instead, my recommendation would be to make use of the command bus and events.
Here's the docs for those features:
http://laravel.com/docs/5.0/bus
http://laravel.com/docs/5.0/events
My recommendation would be to use the following pattern.
There are a few reasons why I like this pattern: Conceptually your commands handle things that are happening right now and events handle things that have just happened. Also, you can easily put command and event handlers onto a queue to be processed later on - this is great for sending emails as you tend not to want to do that in real time as they slow the HTTP request down a fair bit. You can also have multiple event handlers for a single event which is great for separating concerns.
It would be difficult to provide any actual code here as your question more about the concepts of Laravel, so I'd recommend viewing these videos so you get a good idea of how this pattern works:
This one describes the command bus:
https://laracasts.com/lessons/laravel-5-events
This one describes how events work:
https://laracasts.com/lessons/laravel-5-commands
You can opt for the Observer approach to deal with Model Events. For example, here is my
BaseObserver
:Now if I am to create a Product Model, its Observer would look like this:
Regarding listeners, I create an
observers.php
file insideObservers
dir and I include it from theAppServiceProvider
. Here is a snippet from within theobservers.php
file:All of this is regarding
Model Events
.If you need to send an e-mail after a record is created, it would be cleaner to use the Laravel 'other' Events, as you will have a dedicated class to deal with just that, and fire it, when you wish, from the Controller.
The 'other' Events will have much more purpose as the more automated your app becomes, think of all the daily cronjobs you will need at some point. There will be no more cleaner way to deal with that other than 'other' Events.
I might come after the battle, but If you do not want all the fuss of extending classes or creating traits, you might want to give a try to this file exploration solution.
Laravel 5.X solution
Beware the folder you choose to fetch the models should only contain models to make this solution to work
Do not forget to add the
use File
app/Providers/AppServiceProvider.php
Hope it helps you write the less code possible!
You can have multiple listeners on an event. So you may have a listener that sends an email when an article is updated, but you could have a totally different listener that does something totally different—they’ll both be executed.