当用户注册时,我在哪里添加Laravel 5.1中的事件

Jac*_*ham 2 php authentication events laravel laravel-5

我有一个Event::fire();getRegister();Illuminate/Foundation/Auth/RegistersUsers.php

但我知道这不是正确的位置:

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());
}
Run Code Online (Sandbox Code Playgroud)

我如何添加事件,以便它不在Illuminate和AuthController中?

编辑:活动正常.我只需要知道哪个位置最好解雇它?

The*_*pha 5

你有这个:

protected $listen = [
    UserWasRegistered::class => [
        SendActivationEmail::class,
        CreateNewModel::class,
    ],
];
Run Code Online (Sandbox Code Playgroud)

现在就到你的控制台/命令提示符并运行php artisan event:generate,然后去App\EventsApp\Listeners文件夹,找到适当的类,实现你所需要的,因为Laravel会为您生成这些类.

更新: 实际上,无论如何,你的问题并不清楚.您不应该修改它,trait而是可以在任何类中使用该特征并使用该特征的方法.在这种情况下,您可以实现自定义注册.要做到这一点,只需使用App\Http\Controllers\Auth\AuthController和覆盖postregister方法:

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());

    }
}
Run Code Online (Sandbox Code Playgroud)

在此控制器中(App\Http\Controllers\Auth\AuthController),您可以使用它来实现自定义postRegister方法.