在Laravel 5.1中发送邮件的事件

3

我正在尝试在Laravel 5.1中实现事件。当事件被触发时,将发送一封邮件。

我按照以下步骤进行操作:

首先,在EventServiceProvider中,我在$listen中添加了以下内容:

'send.mail' => [
            \App\Listeners\CreateTicketMail::class,
        ],

第二步,创建一个新的事件类 - SendActionEmails
然后在监听器类中为此事件创建一个处理程序 - CreateTicketMail
public function handle(SendActionEmails $event)
{
    dd($event);
}

现在,当我触发这个事件send.mail时,我收到一个错误。
Event::fire('check.fire');

Argument 1 passed to App\Listeners\CreateTicketMail::handle() must be an instance of App\Events\SendActionEmails, none given

另外,我想知道在哪里可以将数据发送到事件中,这些数据将被邮件使用,例如收件人、发件人、主题等。

我找到的一种方法是,在触发事件时将其作为参数传递给fire函数。

Event::fire('check.fire', array($data));

但是,我该如何在监听器中处理这些数据呢?
1个回答

2
你需要将事件对象传递给fire方法,并监听一个类似于事件类名的事件:
在EventServiceProvider中:
SendActionEmails::class => [
    \App\Listeners\CreateTicketMail::class,
],

事件类:

class SendActionEmails {
  public $data;
}

触发事件并传递一些数据:

$event = new SendActionEmails;
$event->data = 'some data';
Event::fire($event);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接