【发布时间】:2016-01-30 14:18:38
【问题描述】:
我的应用程序中有一个事件设置,在用户注册时会发送一封激活电子邮件。没有ShouldQueue 接口,它也能完美运行。但是,当我添加implements ShouldQueue 时,我得到未定义的属性错误:
未定义属性:App\Events\User\UserCreated::$user
我使用的代码如下:
<?php
namespace App\Listeners\User;
use App\Events\User\UserCreated;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Repos\Email\EmailTemplateRepoInterface;
use App\Repos\User\UserRepoInterface;
use Request, Mail, DbView;
class UserCreatedEmail implements ShouldQueue
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct(EmailTemplateRepoInterface $template)
{
$this->template = $template;
}
/**
* Handle the event.
*
* @param UserCreated $event
* @return void
*/
public function handle(UserCreated $event)
{
$user = $event->user;
if($user)
{
if(!$user->status)
{
$user->activation_url = Request::root().'/activate/'.$user->activation_code;
$template = $this->template->findTemplateByName('new_user_activation');
$userArr = $user->toArray();
}
Mail::queue([], [], function ($message) use ($template, $userArr)
{
$message->to($userArr['email'])
->subject($template->subject)
->setBody(DbView::make($template)->with($userArr)->render(), 'text/html');
});
}
}
}
编辑
这也是UserCreated 类:
<?php
namespace App\Events\User;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class UserCreated extends Event
{
use SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($user, $row, $request)
{
$this->user = $user;
$this->request = $request;
$this->row = $row;
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return [];
}
}
谁能解释为什么我尝试将电子邮件排队时会发生这种情况?
【问题讨论】:
-
UserCreated实例没有设置user属性。你能给我们介绍一下课程吗? -
@Dencker - 我已经添加了上面的类。
标签: php laravel laravel-4 laravel-5