用户注册的Laravel 5事件
我试图弄清楚,当我试图调用我的用户注册事件时,为什么没有收到任何反馈。store函数的其他部分工作得很好,但是在尝试调试时,它不会在日志中给我任何错误或任何东西。我已将MAIL_驱动程序设置为登录.env文件,因此不会返回任何内容 有人知道为什么吗 app/Events/userwasristered.php用户注册的Laravel 5事件,laravel,laravel-5,event-handling,laravel-5.2,Laravel,Laravel 5,Event Handling,Laravel 5.2,我试图弄清楚,当我试图调用我的用户注册事件时,为什么没有收到任何反馈。store函数的其他部分工作得很好,但是在尝试调试时,它不会在日志中给我任何错误或任何东西。我已将MAIL_驱动程序设置为登录.env文件,因此不会返回任何内容 有人知道为什么吗 app/Events/userwasristered.php <?php namespace App\Events; use App\Events\Event; use Illuminate\Queue\SerializesModels;
<?php
namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\UserAccount;
class UserWasRegistered extends Event
{
use SerializesModels;
public $userAccount;
/**
* Create a new event instance.
*
* @param UserAccount $user
*/
public function __construct(UserAccount $userAccount)
{
$this->userAccount = $userAccount;
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return [];
}
}
<?php
namespace App\Listeners;
use App\Events\UserWasRegistered;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Mailers\AppMailer;
class SendRegistrationEmail
{
protected $mailer;
/**
* Create the event listener.
*
*/
public function __construct(AppMailer)
{
$this->mailer = $mailer;
}
/**
* Handle the event.
*
* @param UserWasRegistered $event
* @return void
*/
public function handle(UserWasRegistered $event)
{
$this->mailer->sendWelcomeEmailTo($event->user->email);
}
}
<?php
namespace App\Http\Controllers;
use App\UserAccount;
use App\Events\UserWasRegistered;
use App\Http\Requests\UserAccountCreatedPostRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class UserAccountsController extends Controller
{
/**
* Stores the user account saved in the create form to the database.
*
* @param UserAccountCreatedPostRequest $request
* @param UserAccount $userAccount
* @return \Illuminate\Http\RedirectResponse
*/
public function store(UserAccountCreatedPostRequest $request, UserAccount $userAccount)
{
$userAccountCreated = $userAccount->create($request->all());
event(new UserWasRegistered($userAccountCreated));
if ($userAccountCreated) {
flash()->success('Success', 'The user account has been successfully created!');
} else {
flash()->error('Error', 'The user account could not be successfully created!');
}
return redirect()->to(route('app.user-accounts.index'));
}
}
您需要注册侦听器
在文件app/Providers/EventServiceProvider.php
中填写属性$listen
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'App\Events\UserWasRegistered' => [
'App\Listeners\SendRegistrationEmail',
],
];
您需要注册侦听器
在文件app/Providers/EventServiceProvider.php
中填写属性$listen
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
'App\Events\UserWasRegistered' => [
'App\Listeners\SendRegistrationEmail',
],
];
将您的app/Listeners/SendRegistrationEmail.php更改为以下,以便它发送电子邮件(或您案例中的日志)
将你的app/Listeners/SendRegistrationEmail.php更改为下方,以便它发送电子邮件(或在你的案例中记录)
尝试dd()而不是var\u dump()以查看它是否工作。尝试dd()而不是var\u dump()以查看它是否工作。这是您所问不同问题的第三个答案
查看错误消息:
Argument 1 passed to App\Mailers\AppMailer::sendWelcomeEmailTo() must
be an instance of App\Mailers\UserAccount, string given
您需要向用户帐户传递电子邮件,而不仅仅是电子邮件,如下所示:
$this->mailer->sendWelcomeEmailTo($event->userAccount);
这是您提出的不同问题的第三个答案
查看错误消息:
Argument 1 passed to App\Mailers\AppMailer::sendWelcomeEmailTo() must
be an instance of App\Mailers\UserAccount, string given
您需要向用户帐户传递电子邮件,而不仅仅是电子邮件,如下所示:
$this->mailer->sendWelcomeEmailTo($event->userAccount);
很抱歉,我忘了在我的帖子中包含这个,所以我更新了它。这是我已经准备好的东西。所以这不是问题所在。所以,我不知道怎么了,一切看起来都是对的。我很抱歉,但是我忘了在我的帖子中包含这个,所以我更新了它。这是我已经准备好的东西。所以这不是问题所在。所以,我不知道怎么了,一切看起来都是对的。很抱歉。在触发事件之前,如果添加($userAccountCreated),您会看到什么?UserAccount对象。一切看起来都正常。不过,var_dump不会向您显示任何内容。您是否尝试发送电子邮件或记录到文件?是的,我在.env文件中的邮件设置如顶部所述被记录。我了解您的电子邮件设置,但您没有在句柄函数中发送电子邮件。在触发事件之前,如果您添加($userAccountCreated),您会看到什么?UserAccount对象。一切看起来都正常。不过,var_dump不会向您显示任何内容。您是否尝试发送电子邮件或登录到文件?是的,我在.env文件中有我的邮件设置,如顶部所述。我了解您的电子邮件设置,但您没有在handle函数中发送电子邮件。我更新了我的帖子,问题是:未定义属性:App\Events\userwasregated::$useryou仍然有问题吗?是的因为我在上面的注释中得到了错误,所以我刚刚更新了代码。它应该是$event->userAccount->email而不是$event->user->email。我更新了我的帖子,问题是:未定义的属性:App\Events\userwasregated::$useryou仍然有问题吗?是的,因为我在上面的评论中发现了错误。刚刚更新了代码。应该是$event->userAccount->email而不是$event->user->email。好的,我道歉。我不断地犯错误,因为它不起作用。我现在在AppMailer.php第35行收到:FatalThrowableError:类型错误:传递给App\Mailers\AppMailer::sendWelcomeEmailTo()的参数1必须是App\Mailers\UserAccount的实例,给定的App\UserAccount的实例,在第31行的/home/vagrant/Projects/repositories/myapp/app/Listeners/SendRegistrationEmail.php中调用,您可以在错误消息中看到,它显示键入error。您将错误的实例传递给sendWelcomeEmailTo()函数,或者sendWelcomeEmailTo不需要正确的实例。好的,我道歉。我不断地犯错误,因为它不起作用。我现在在AppMailer.php第35行收到:FatalThrowableError:类型错误:传递给App\Mailers\AppMailer::sendWelcomeEmailTo()的参数1必须是App\Mailers\UserAccount的实例,给定的App\UserAccount的实例,在第31行的/home/vagrant/Projects/repositories/myapp/app/Listeners/SendRegistrationEmail.php中调用,您可以在错误消息中看到,它显示键入error。您将错误的实例传递给sendWelcomeEmailTo()函数,或者sendWelcomeEmailTo不需要正确的实例。嗯,不确定为什么会得到下一票,但实际上这是其中一个问题的解决方案。嗯,不确定为什么会得到下一票,但实际上这是其中一个问题的解决方案。