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

时间:2015-07-03 17:12:40

标签: php events laravel authentication 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());
}

我如何添加事件,使其不在Illuminate和AuthController中?

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

1 个答案:

答案 0 :(得分:5)

你有这个:

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

现在只需转到控制台/命令提示符并运行php artisan event:generate,然后转到App\EventsApp\Listeners文件夹,找到相应的类并根据需要实施,因为{{1}将为你生成这些类。

<强>更新 实际上,无论如何,你的问题并不清楚。您不应该修改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()); } } 中,您可以使用它来实现自定义(App\Http\Controllers\Auth\AuthController)方法。