简体   繁体   中英

Where do I add events in Laravel 5.1 when user registers

I have an Event::fire(); on getRegister(); in Illuminate/Foundation/Auth/RegistersUsers.php

But I know this is not the correct location for it:

public function postRegister(Request $request)
{
    $validator = $this->validator($request->all());

    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );
    }

    Auth::login($this->create($request->all()));

    Event::fire(new UserWasRegistered(Auth::id()));

    return redirect($this->redirectPath());
}

How I add the event so it's not in Illuminate and in AuthController?

Edit: The event is working perfectly. I just need to know which location is best to fire it?

You have this:

protected $listen = [
    UserWasRegistered::class => [
        SendActivationEmail::class,
        CreateNewModel::class,
    ],
];

Now just go to your console/command prompt and run php artisan event:generate then go to App\\Events and App\\Listeners folder and find the appropriate classes and implement as you need, because Laravel will generate those classes for you.

Update: Actually, your question was not clear, anyways. You should not modify that trait instead you can use that trait in any class and use the methods of that trait. In This case, you can implement your custom Registration. To do this, just use the App\\Http\\Controllers\\Auth\\AuthController and override the postregister method:

namespace App\Http\Controllers\Auth;

// ...

class AuthController extends Controller
{
    use AuthenticatesAndRegistersUsers;

    // Other methods ...

    public function postRegister(Request $request)
    {
        // Do the coding here
        $validator = $this->validator($request->all());

        if ($validator->fails()) {
            $this->throwValidationException(
                $request, $validator
            );
        }

        Auth::login($this->create($request->all()));

        // Fire Event Here...

        return redirect($this->redirectPath());

    }
}

In this controller (App\\Http\\Controllers\\Auth\\AuthController) which you may use to implement your custom postRegister method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM