Laravel 5 Eventing For UserRegistering



我试图弄清楚为什么当我尝试调用我的用户正在注册的事件时没有得到任何反馈。存储功能的其他部分工作正常,但是在尝试调试时,它不会在我的日志中给我任何错误或任何东西。我已将MAIL_DRIVER设置为登录我的 .env 文件,因此我不会返回任何内容。

有人知道为什么吗?

app/Events/UserWasRegistrered.php

<?php
namespace AppEvents;
use AppEventsEvent;
use IlluminateQueueSerializesModels;
use IlluminateContractsBroadcastingShouldBroadcast;
use AppUserAccount;
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 [];
    }
}

app/Listeners/SendRegistrationEmail.php

<?php
namespace AppListeners;
use AppEventsUserWasRegistered;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsQueueShouldQueue;
use AppMailersAppMailer;
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);
    }
}

app/Http/Controllers/UserAccountsController.php

<?php
namespace AppHttpControllers;
use AppUserAccount;
use AppEventsUserWasRegistered;
use AppHttpRequestsUserAccountCreatedPostRequest;
use AppHttpRequests;
use AppHttpControllersController;
class UserAccountsController extends Controller
{
     /**
     * Stores the user account saved in the create form to the database.
     *
     * @param UserAccountCreatedPostRequest $request
     * @param UserAccount $userAccount
     * @return IlluminateHttpRedirectResponse
     */
    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

<?php
namespace AppProviders;
use IlluminateContractsEventsDispatcher as DispatcherContract;
use IlluminateFoundationSupportProvidersEventServiceProvider as ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        'AppEventsUserWasRegistered' => [
            'AppListenersSendRegistrationEmail',
        ],
    ];
    /**
     * Register any other events for your application.
     *
     * @param  IlluminateContractsEventsDispatcher  $events
     * @return void
     */
    public function boot(DispatcherContract $events)
    {
        parent::boot($events);
        //
    }
}

更新

由于以下原因,我收到以下错误。

AppMailer 中的 FatalThrowableError .php第 35 行:类型错误:传递给 App\Mailers\AppMailer::sendWelcomeEmailTo() 的参数 1 必须是 App\Mailers\UserAccount 的实例,给定字符串,在第 31 行的/home/vagrant/Projects/repository/myapp/app/Listeners/SendRegistrationEmail.php 中调用

应用\侦听器\发送注册电子邮件

<?php
namespace AppListeners;
use AppEventsUserWasRegistered;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsQueueShouldQueue;
use AppMailersAppMailer;
class SendRegistrationEmail
{
    protected $mailer;
    /**
     * Create the event listener.
     *
     */
    public function __construct(AppMailer $mailer)
    {
        $this->mailer = $mailer;
    }
    /**
     * Handle the event.
     *
     * @param  UserWasRegistered  $event
     * @return void
     */
    public function handle(UserWasRegistered $event)
    {
        $this->mailer->sendWelcomeEmailTo($event->userAccount->email);
    }
}

这是你提出的不同问题的第三个答案。

查看错误消息:

Argument 1 passed to AppMailersAppMailer::sendWelcomeEmailTo() must
be an instance of AppMailersUserAccount, string given

您需要传递用户帐户,而不仅仅是电子邮件,如下所示:

$this->mailer->sendWelcomeEmailTo($event->userAccount); 

你需要注册监听器

https://laravel.com/docs/5.2/events#registering-events-and-listeners

在文件中app/Providers/EventServiceProvider.php填写属性$listen

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    'AppEventsUserWasRegistered' => [
        'AppListenersSendRegistrationEmail',
    ],
];

将您的应用程序/侦听器/发送注册电子邮件.php更改为下方,以便它发送电子邮件(或日志(在您的情况下)

<?php
namespace AppListeners;
use AppEventsUserWasRegistered;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsQueueShouldQueue;
use Mail;
class SendRegistrationEmail
{
    /**
     * Create the event listener.
     *
     */
    public function __construct()
    {
        //
    }
    /**
     * Handle the event.
     *
     * @param  UserWasRegistered  $event
     * @return void
     */
    public function handle(UserWasRegistered $event)
    {
        Mail::raw('You have been successfully registered to the site', function ($message) use ($event) {
            $message->to($event->userAccount->email);
            $message->subject('Welcome');
        });
    }
}

尝试使用 dd() 而不是 var_dump() 看看它是否正常工作。

相关内容

  • 没有找到相关文章

最新更新