It is possible to fire a Event on Mail sent in Laravel using view name as id ? for example :
Event::listener('emails.register', function($mail){});
Subscribe event litener with Event::listen (not listener):
Event::listen('emails.register', function($mail){
// do something with $mail
});
then fire the event:
// using Facade
Event::fire('emails.register', $mail);
// or resolving dispatcher from the IoC container
App::make('events')->fire('emails.register', $mail);
Related
How can I send email and SMS together using Laravel 5.5 ?
Should I use mailable and SMS service or Notification with SMS and How ?
You can create an event in laravel's App/Events folder.
put all the code for send mail and sms with there configuration, just call that event wherever you want to hit sms or mail.
You may create one function which will include both notify() method for an email and dispatch() method for message
example
public function Notification(Model $tender)
{
$user = access()->user();
/* Send confirmation email */
$tender->notify(new Email($user));
/* Send confirmation message */
Send::dispatch($user);
}
Then you will just call a single function that is Notification() which will do both sending email and message
in Laravel 5.6
I have an event named DocumentSend,
And i have many Listeners Like (SendEmail, SendNotification, SendSMS),
listeners are optional (depends on document type and defined by user),
now the question is:
How can i call for example DocumentSend event with just SendSMS listener or DocumentSend with all the listeners?
I hope you get my mean,and tell me the best practice for my issue.
Thanks in advance
Well, the simple answers is - you can't. When you fire event all registered listeners will listen to this event and all of them will be launched.
However nothing stops you to prevent running code from listener.
For example you can fire event like this:
event(new DocumentSend($document, true, false, false));
and define constructor of DocumentSend like this:
public function __construct($document, $sendEmail, $sendNotification, $sendSms)
{
$this->document = $document;
$this->sendEmail = $sendEmail;
$this->sendNotification = $sendNotification;
$this->sendSms = $sendSms;
}
and now in each listener you can just verify correct variable, so for example in SendEmail listener in handle you can do it like this:
public function handle(DocumentSend $event)
{
if (!$event->sendSms) {
return;
}
// here your code for sending
}
similar you can do for other listeners.
Of course this is just example - you don't have to use 4 variables. You can set some properties to $document only to mark how it should be sent.
I'm trying to integrate Laravel-Vue-Pusher based notification and laravel's broadcasting documentation has been very helpful.
So we create an laravel event, trigger and broadcast it on a Pusher channel.
On the Javascript side, we use Echo to listen for event broadcasts.
Here's some example-code for from the documentation:
Echo.private(`order.${orderId}`)
.listen('ShippingStatusUpdated', (e) => {
console.log(e.update);
});
I tried using it in Vue and it threw an ReferenceError: orderId is not defined.
On the Laravel side, here's the event that's broadcasting it on that channel:
public function broadcastOn()
{
return new PrivateChannel('order.'.$this->order->id);
}
Event is fired successfully and it also gets logged in Pusher Dashboard.
But i'm unable to figure out why i'm getting missing orderId error in Vue. Any help would be appreciated.
How to trigger a event for the Mail service?
Example
If mail template code equals mail.order.complete add bcc?
OctoberCMS is built on Laravel 5.1 and to do handle mail events you will need handle it within your Plugin.php file.
For example to handle the process just before the mails being sent:
class Plugin extends PluginBase
{
[...]
public function boot()
{
Event::listen('mailer.sending', function(){
});
}
}
To learn more about events in OctoberCMS and Laravel you can check out these links:
https://octobercms.com/docs/services/events
https://laravel.com/docs/5.1/events
https://laravel.com/docs/5.1/mail
I made the authorization and authentication via facebook like here:
http://symfony.com/doc/current/cookbook/security/custom_authentication_provider.html
and it works
Now I want to make my own event, this event will do something when the user authenticates using facebook. For example-will redirect the user to the home page.
I did it like this
http://symfony.com/doc/current/components/event_dispatcher/introduction.html
So I have this class
http://pastebin.com/2FTndtL4
I do not know how to implement it, what am I supposed to pass as an argument to the constructor
It's really simple. Symfony 2 event system is powerful, and service tags will do the job.
Inject the dispatcher into the class where you want to fire the event. The service id is event_dispatcher;
Fire the event with $this->dispatcher->dispatch('facebook.post_auth', new FilterFacebookEvent($args)) when needed;
Make a service that implements EventSubscriberInterface, defining a static getSubscribedEvents() method. Of course you want to listen to facebook.post_auth event.
So your static method will look like:
static public function getSubscribedEvents()
{
return array(
'facebook.post_auth' => 'onPostAuthentication'
);
}
public function onPostAuthentication(FilterFacebookEvent $event)
{
// Do something, get the event args, etc
}
Finally register this service as a subscriber for the dispatcher: give it a tag (eg. facebook.event_subscriber), then make a RegisterFacebookEventsSubscribersPass (see this tutorial). You compiler pass should retrieve all tagged services and inside the loop should call:
$dispatcher = $container->getDefinition('event_dispatcher');
$subscribers = $container->findTaggedServiceIds('facebook.event_subscriber');
foreach($subscribers as $id => $attributes) {
$definition->addMethodCall('addSubscriber', array(new Reference($id)));
}
This way you can quick make a subscriber (for logging, for example) simply tagging your service.
Event object is just some kind of state/data storage. It keeps data that can be useful for dispatching some kind of events via Subscribers and/or Listeners. So, for example, if you wanna pass facebook id to your Listener(s) - Event is the right way of storing it. Also event is the return value of dispatcher. If you want to return some data from your Listener/Subscriber - you can also store it in Event object.