samedi 12 mars 2016

Laravel 5 Eventing For UserRegistering

I'm trying to figure out why I'm not getting any feedback when I'm trying to call my event of the user being registered. The other parts of the store function work just fine however it doesn't give me any errors in my log or anything when trying to debug. I have my MAIL_DRIVER set to log in my .env file so I'm not getting anything returned.

Anyone know why?

app/Events/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 $user;

    /**
     * Create a new event instance.
     *
     * @param UserAccount $user
     */
    public function __construct(UserAccount $user)
    {
        $this->user = $user;
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return [];
    }
}

app/Listeners/SendRegistrationEmail.php

<?php

namespace App\Listeners;

use App\Events\UserWasRegistered;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Mailer;

class SendRegistrationEmail
{
    /**
     * Create the event listener.
     *
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  UserWasRegistered  $event
     * @return void
     */
    public function handle(UserWasRegistered $event)
    {
        var_dump('Email ' . $event->user->email . ' that they have been registered for the site.');
    }
}

app/Http/Controllers/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'));
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire