Laravel 5.1事件ShouldQueue

时间:2015-08-17 10:57:44

标签: php laravel laravel-5

我有以下监听器设置,在提交表单时向用户发送电子邮件通知。它完美地工作,除非我添加implements ShouldQueue它返回错误。我正在使用Iron.io来解雇事件。

如果删除implements ShouldQueue,则可以正常使用。但是,加载页面需要更长的时间。 implements ShouldQueue有什么影响?

<?php

namespace App\Listeners\Timesheet;

use App\Events\Timesheet\TimesheetCreated;
use App\Repos\Email\EmailTemplateRepoInterface;
use App\Repos\User\UserRepoInterface;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Mail, DbView, Auth;

class EmailTimesheetCreationConfirmation implements ShouldQueue
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct(EmailTemplateRepoInterface $template, UserRepoInterface $user)
    {
        $this->template = $template;
        $this->user = $user;
    }

    /**
     * Handle the event.
     *
     * @param  TimesheetCreated  $event
     * @return void
     */
    public function handle(TimesheetCreated $event)
    {
        $timesheet = $event->timesheet;

        $this->dispatchEmail($timesheet->submitted_by, 1); // send submittor email
        $this->dispatchEmail($timesheet->employer_id, 2); // send employer email
        $this->dispatchEmail($timesheet->supervisor_id, 3); // send supervisor email
    }

    /**
     * 
     *
     * @param  TimesheetCreated  $event
     * @return void
     */
    public function dispatchEmail($id, $templateId)
    {
        $user = $this->user->getUserNotificationSettingsById($id);

        if($user)
        {
            if($user->notify_create_timesheet)
            {
                $template = $this->template->findTemplateById(1);

                Mail::queue([], [], function ($message) use ($template, $user) 
                {
                    $message->to($user->email)
                        ->subject($template->subject)
                        ->setBody(DbView::make($template)->with($user->toArray())->render(), 'text/html');
                });
            }
        }
    }
}

我使用implements ShouldQueue时遇到的错误如下:

exception 'ErrorException' with message 'Undefined property: App\Events\Timesheet\TimesheetCreated::$timesheet' in /home/vagrant/Code/App/public/app/Listeners/Timesheet/EmailTimesheetCreationConfirmation.php:33

0 个答案:

没有答案