Laravel 5用户注册事件

时间:2016-03-12 21:15:40

标签: laravel laravel-5 event-handling laravel-5.2

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

任何人都知道为什么?

应用/活动/ UserWasRegistrered.php

<?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 [];
    }
}

应用/听众/ SendRegistrationEmail.php

<?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);
    }
}

应用/ HTTP /控制器/ UserAccountsController.php

<?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'));
    }
}

应用程序/提供者/ EventServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        'App\Events\UserWasRegistered' => [
            'App\Listeners\SendRegistrationEmail',
        ],
    ];

    /**
     * Register any other events for your application.
     *
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
     * @return void
     */
    public function boot(DispatcherContract $events)
    {
        parent::boot($events);

        //
    }
}

更新

由于某种原因,我遇到以下错误。

AppMailer.php第35行中的

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

应用\监听\ SendRegistrationEmail

<?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 $mailer)
    {
        $this->mailer = $mailer;
    }

    /**
     * Handle the event.
     *
     * @param  UserWasRegistered  $event
     * @return void
     */
    public function handle(UserWasRegistered $event)
    {
        $this->mailer->sendWelcomeEmailTo($event->userAccount->email);
    }
}

4 个答案:

答案 0 :(得分:1)

您需要注册听众

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 = [
    'App\Events\UserWasRegistered' => [
        'App\Listeners\SendRegistrationEmail',
    ],
];

答案 1 :(得分:1)

将您的app / Listeners / SendRegistrationEmail.php更改为以下内容,以便发送电子邮件(或记录您的案例)

<?php

namespace App\Listeners;

use App\Events\UserWasRegistered;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
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');
        });

    }
}

答案 2 :(得分:1)

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

查看错误消息:

Argument 1 passed to App\Mailers\AppMailer::sendWelcomeEmailTo() must
be an instance of App\Mailers\UserAccount, string given

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

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

答案 3 :(得分:0)

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

相关问题